How to query user with posts in Laravel

前端 未结 2 1240
走了就别回头了
走了就别回头了 2021-01-03 08:22

I\'m new to Laravel. I want to display all post including the user who posted the post. I have a simple query which displays all post, and displays selected post using post

2条回答
  •  余生分开走
    2021-01-03 08:47

    You can simply use also this.

    public function($id){
    $posts = Posts::where('uid','=',auth()->user()->id)->findOrFail($id);
    return view('your_blade.php',compact('posts'));
    
    OR either you can return this
    
    return response()->json([$posts], 200);
    
    
    }
    

    Note: don't forgot to add trait also use App\Posts;

提交回复
热议问题