YaSh Chaudhary YaSh Chaudhary - 3 months ago 8
PHP Question

dd function not able to fetch content from database

I am building a post system in which user can post on his wall.Posts are created successfully and are saving into database but it can't be fetched from the database.

I've also relationship between user and posts.

my postcontroller:

public function getmyplace()
{

$posts=Post::where(function($query){
return $query->where('user_id',Auth::user()->id)
->orWhere('user_id',Auth::user()->friends()->lists('id')
);
})
->orderBy('created_at','desc')->get();
dd($posts);

$accounts=Account::orderBy('updated_at','desc')->limit(1)->get();

return view('myplace',['posts'=>$posts,'accounts'=>$accounts]);
}
public function postCreatePost(Request $request)

{
Auth::user()->posts()->create([

'body' => $request->input('body'),

]);

return redirect()->route('myplace');
}


my database:

enter image description here

output:

enter image description here

Answer

"Return" statement is not applyed in nested query builder and Collection::lists() method return array. Try this:

$posts = Post::where(function($query){
        $query->where('user_id',Auth::user()->id)
            ->orWhereIn('user_id',Auth::user()->friends()->lists('id'));
    })
    ->orderBy('created_at','desc')->get();
dd($posts);