Issue
I have a table to store my articles. I have no pictures in this table, and my photos are in another table. I have added 5 posts and each post has 3 pictures. Now I want to get 5 posts and each post only takes 1 out of 3 pictures of it, how to do ?? i need help, thanks
Solution
Use Laravel's relationship functions, Add this to your Post model:
public function images()
{
return $this->hasMany(YourImageModel::class);
}
And for your query use this:
$post = Post::find($id)->with('images')->first();
$allImages = $post->images;
$firstImage = $post->images->first();
If you want all posts with images:
$posts = Post::with('images')->get();
In your blade:
@foreach($posts as $post)
{{ $post->images->first() }}
@endforeach
Answered By - ibrahim-dogan
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.