How to display the user record from the database in Laravel?

0 like 0 dislike
5 views
We need to display the user posts on his page.

In User.php added relation

public function post(){
return $this->hasMany('App\\post');
}

The output on the page using @foreach($posts as $post)
{{ $post->content }}

Now displays all posts from all users.
by | 5 views

1 Answer

0 like 0 dislike
If in the following diagram something doesn't work look for a problem in your code:
// User Model public function posts () { return $this->hasMany(Post::class); } // Controller (if needed) public function index () { $posts = Auth::user()->posts; } // View @foreach($posts as $post) {{ $post->content }} @endforeach
by

Related questions

0 like 0 dislike
4 answers
0 like 0 dislike
3 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
5 answers
110,608 questions
257,186 answers
0 comments
33,924 users