Reputation: 4166
I work on a blog project, which have relation many to many between "posts" and "cats" tables (with pivote table)
posts:
id
title
topic
cats:
id
name
cat_post:
post_id
cat_id
I prepare models correctly, so, how to select all posts in specific category? I tried:
Cat::with('post')->where('id', '=', '3')->get();
and
Post::with('cat')->whereId('3')->get();
but nothing yet,
Upvotes: 0
Views: 75
Reputation: 146219
This is better/shorter:
$cats = Cat::with('post')->find(3);
Update:
$cats = Cat::with(array('post' => function($q) { // make sure posts not post
$q->orderBy('id', 'desc'); // order the posts
}))->find(3); // No need to order a single model
Upvotes: 2
Reputation: 4166
just found the answer
$cats = Cat::with('post')->whereId(3)->first();
then retrive it in blade:
@foreach($cats->post as $post)
{{$post->title}}
@endforeach
thanks,
Upvotes: 0