using the query builder for a many to many relationship? I have a campaigns table and and an items table, and the pivot table is campaign_item
is it possible to use the query builder to get all items that belong to a campaign?
I ask because I need to chain some queries on it, but in the docs it only shows how to get the relationship through eloquent
I search online and there are a few threads about the subject but I'm still not sure how to go about it
What do you mean by chaining queries? You mean chaining other relationships? In that case you can do something like this
// Let's say an item has a user as well
$campaign = Campaign::with('items.user')->find(1);;
foreach ($campaign->items as $item) {
$item->user;
}
In all other cases you need to use a join to make this work
@BOBBYBOUWMANN - I need to get all items that belong to one campaign, and chain other queries on them like whereMonth() and orderBy()
how would a join look like in laravel?
You can still chain other things like so
$campaign = Campaign::with([
'items' => function ($query) {
$query->whereMonth('created_at',2);
},
'items.user',
])->where('active', 1)->find(1);
Note that if you want to order the campaign by the items you have to use a join!
For joins you can check this: https://laravel.com/docs/5.8/queries#joins
If you use eloquent, you can do something like:
$campaign = Campaign::with(['items' => function($query){
$query->whereMonth('somemonth');
$query->orderBy('something', 'asc');
}])->find(1);
Reference:
https://laravel.com/docs/5.8/eloquent-relationships#constraining-eager-loads
When it comes to Query Builder without eloquent you would probably need to use joins, since it doesn't have relations. But if I understood you correctly, wouldn't using eloquent work with constraints?
basically I had a belongsTo relationship and this was my code
$files = File::where('campaign_id', $id);
if ($month = request('month')) {
$files->whereMonth('created_at', Carbon::parse($month)->month);
}
if ($year = request('year')) {
$files->whereYear('created_at', $year);
}
$files = $files->orderBy('created_at', 'desc')->paginate(10);
this worked
but I now changed to a belongsToMany relationship with a pivot table and not sure how to implement it on the front end
Can one file relate to many campaigns now then?
You could fetch the campaign, and then fetch the files
$camapaign = Campaigns::find($id);
$files = $campaign->files(); // This would be the same query as
// $files = File::where('campaign_id', $id);
// Then go on as usual and add your extra queries on the $files
Another way would be to utilize there whereHas function:
ref: https://laravel.com/docs/5.8/eloquent-relationships#querying-relationship-existence
$files = File::whereHas('campaign', function($query) use ($id){
$query->where('campaign_id', $id);
});
// Then go on as usual and add your extra queries on the $files
Hope I understood your relations.
Thankyou so much, got so many solutions
-Mayur .
Please sign in or create an account to participate in this conversation.