$jobs = EventJob::with(['applications' => function ($query) {
$query->where('accepted', false);
}, 'job' => function($query) {
$query->where('division_id', 1);
}, 'event' => function($query) use ($date) {
$query->where('date', '>=', $date);
}])->get();
Complete overhaul of the question.
I wan't only the EventJob elements where the conditions in the $query are true but I get all the elements.
Seems like the function you're looking for is whereHas, it is documented here.
Your code would then be :
$jobs = EventJob::whereHas('applications', function ($query) {
$query->where('accepted', false);
})->whereHas('job', function($query) {
$query->where('division_id', 1);
})->wherehas('event', function($query) use ($date) {
$query->where('date', '>=', $date);
})->get();
Related
I have this query
$t_items_between = TransferItem::whereHas('transfer', function ($query) use($old_until_date, $inventory_id, $old_from_date_interval) {
$query->where('document_date', '>=', $old_from_date_interval);
$query->where('document_date', '<=', $old_until_date);
$query->where('to_inventory_id', $inventory_id);
})
->with(['transfer' => function($query) use($old_until_date, $inventory_id, $old_from_date_interval) {
$query->where('document_date', '>=', $old_from_date_interval);
$query->where('document_date', '<=', $old_until_date);
$query->where('to_inventory_id', $inventory_id);
}
])
->whereIn('item_id', $subset)
->addSelect(['quantity' => TransferItem::selectRaw('sum(quantity)')
->groupBy('item_stock_id')
->from((new TransferItem)->getTable() . ' as ti')
->whereColumn('ti.item_stock_id', (new TransferItem)->getTable() . '.item_stock_id')
])
->get();
So TransferItem is the model of the table transfer_items. transfer_items has 4 columns: id, transfer_id, item_stock_id and quantity.
What I am trying to do is to sum the quantity of each result, grouping by the item_stock_id, but the query above doesn't work. How would I approach it?
try using map
$t_items_between = TransferItem::whereHas('transfer', function ($query) use($old_until_date, $inventory_id, $old_from_date_interval) {
$query->where('document_date', '>=', $old_from_date_interval);
$query->where('document_date', '<=', $old_until_date); $query->where('to_inventory_id', $inventory_id);
})
->with(['transfer' => function($query) use($old_until_date, $inventory_id, $old_from_date_interval) {
$query->where('document_date', '>=', $old_from_date_interval);
$query->where('document_date', '<=', $old_until_date); $query->where('to_inventory_id', $inventory_id);
}
])
->whereIn('item_id', $subset)
->get()
->groupBy('item_stock_id')
->map(function ($q) {
return $q->sum('quantity');
});
Visit https://laravel.com/docs/9.x/collections
Here my code :
$prestations = Prestation::with('service','facility','conciergeries.network')
->whereHas('service', function ($query) use ($searchService) {
$query->addSelect('id','name')->where('name', 'regexp', "/$searchService/i");
})
->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->addSelect('id','name')->where('name', 'regexp', "/$searchPartenaire/i");
})
->whereHas('conciergeries.network', function ($query) use ($searchFiliale) {
$query->addSelect('id','name')->where('name', 'regexp', "/$searchFiliale/i");
})
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->paginate(50);
I want get only names and ids of "service", "facility" and "conciegeries.network".
I tried to use select('id','name'); and select(['id','name']); or addSelect('id','name'); and pluck('id', 'name'); but i get all datas.
Do you have alternatives solutions ?
Thank you !
Select needs to be added to with, not whereHas. Try this instead:
Prestation::with(['service' => function($query) { $query->select(['id','name']);},
'facility' => function($query) { $query->select(['id','name']);},
'conciergeries.network' => function($query) { $query->select(['id','name']);}
])
here I have a code that I query inside a relationship that I want just 1 part to be executed if the user send the variable to me if not that query didn't run at all here is my code I commented in my code where I want my condition:
$bed_count = $request->get('bed_count');
$data = Accommodation::with(['city','accommodationRoomsLimited.roomPricingHistorySearch' =>function($query) use($from_date,$to_date){
$query->whereDate('from_date', '<=', $from_date);
$query->whereDate('to_date', '>=', $to_date);
},'accommodationRoomsLimited' => function($q) use($bed_count){
//here is I want my query to be executed if the user sends bed_count if not the code should skip this part
$q->orWhere('bed_count',$bed_count);
}])
Not clear what are you trying to achieve.
If you want to eager load accommodationRoomsLimited all the time,
But when user has provided bed count, you need to filter those accommodationRoomsLimited with this bed count.
If that's so
$bed_count = $request->get('bed_count');
$data = Accommodation::with([
'city',
'accommodationRoomsLimited.roomPricingHistorySearch' => function($query) use($from_date,$to_date) {
$query->whereDate('from_date', '<=', $from_date);
$query->whereDate('to_date', '>=', $to_date);
},
'accommodationRoomsLimited' => function($q) use($bed_count) {
if ($bed_count) {
$q->where('bed_count',$bed_count);
}
}
]);
If you only want to eager load accommodationRoomsLimited only when user has provided bed count.
Then
$bed_count = $request->get('bed_count');
$data = Accommodation::with([
'city',
'accommodationRoomsLimited.roomPricingHistorySearch' => function($query) use($from_date,$to_date) {
$query->whereDate('from_date', '<=', $from_date);
$query->whereDate('to_date', '>=', $to_date);
},
])
->when($bed_count, function ($query, $bed_count) {
$query->with([
'accommodationRoomsLimited' => function($q) use($bed_count) {
$q->where('bed_count',$bed_count);
}
]);
});
I am doing the following request to extract some data from my database. The expected result is wrong because some parenthesis is not well placed in the SQL generated request:
$data = Ov::with([
'masters' => function ($query) use ($devicesArrayOnlyLKUAToZero) {
$query->whereIn('macAddress', $devicesArrayOnlyLKUAToZero)
->orWhere('lastKnownUpAt','<>', '0');
}
])->where('ovId', '=', $ovId)->get();
The dedicated sql generated by Laravel is: (see with Laravel debugbar):
select * from `masterequipments` where `masterequipments`.`id_ov_foreign_key` in ('38') and `macAddress` in ('e8:e7:32:c1:e6:48', 'e8:e7:32:bc:b0:94', 'e8:e7:32:e4:8e:68', 'e8:e7:32:bc:a7:70', '00:e0:b1:fe:ef:a5', 'e8:e7:32:bc:a7:a4', '2c:fa:a2:10:79:74', 'e8:e7:32:b9:6d:1d', '00:e0:b1:ee:58:2d', '00:e0:b1:9d:2c:44', '00:e0:b1:b5:e6:00', '00:e0:b1:72:34:86', '00:e0:b1:fe:ee:8d', '00:e0:b1:79:53:52', '00:e0:b1:fe:f0:bd', '00:e0:b1:75:fa:8a', 'e8:e7:32:98:80:22', '00:e0:b1:75:00:8a') or `lastKnownUpAt` <> '0'
This is wrong because 2 () are missed. I would like to have:
select * from `masterequipments` where `masterequipments`.`id_ov_foreign_key` in ('38') and (`macAddress` in ('e8:e7:32:c1:e6:48', 'e8:e7:32:bc:b0:94', 'e8:e7:32:e4:8e:68', 'e8:e7:32:bc:a7:70', '00:e0:b1:fe:ef:a5', 'e8:e7:32:bc:a7:a4', '2c:fa:a2:10:79:74', 'e8:e7:32:b9:6d:1d', '00:e0:b1:ee:58:2d', '00:e0:b1:9d:2c:44', '00:e0:b1:b5:e6:00', '00:e0:b1:72:34:86', '00:e0:b1:fe:ee:8d', '00:e0:b1:79:53:52', '00:e0:b1:fe:f0:bd', '00:e0:b1:75:fa:8a', 'e8:e7:32:98:80:22', '00:e0:b1:75:00:8a') or `lastKnownUpAt` <> '0')
Use advanced where clauses to accomplish it.
$data = Ov::with([
'masters' => function ($query) use ($devicesArrayOnlyLKUAToZero) {
$query->where(function ($query) use ($devicesArrayOnlyLKUAToZero) {
$query->whereIn('macAddress', $devicesArrayOnlyLKUAToZero);
})
->orWhere(function($query) {
$query->where('lastKnownUpAt','<>', '0');
});
}
])->where('ovId', '=', $ovId)->get();
UPDATE
$data = Ov::with([
'masters' => function ($query) use ($devicesArrayOnlyLKUAToZero) {
$query->where(function ($query) use ($devicesArrayOnlyLKUAToZero) {
$query->whereIn('macAddress', $devicesArrayOnlyLKUAToZero);
})
->orWhere('lastKnownUpAt','<>', '0');
}
])->where('ovId', '=', $ovId)->get();
Yo need to do something like this:
$data = Ov::with([
'masters' => function ($query) use ($devicesArrayOnlyLKUAToZero) {
$query->where(function ($query) use ($devicesArrayOnlyLKUAToZero) {
$query->whereIn('macAddress', $devicesArrayOnlyLKUAToZero);
})
->orWhere(function($query) {
$query->where('lastKnownUpAt','<>', '0');
});
}
])->where('ovId', '=', $ovId)->get();
This article explain it well http://laraveldaily.com/and-or-and-brackets-with-eloquent/
I'm struggling to understand how to use Eloquent/Query Builder with relationships.
I have the following:
$plantLots = PlantLot::with('controlNumber')
->whereHas('controlNumber', function ($q) use ($fromDate, $toDate) {
$q->where('created_at', '>=', $fromDate);
if($toDate != '') {
$q->where('created_at', '=<', $toDate);
}
$q->groupBy('creator_id');
})->get();
I want to group by creator_id, but I still just get a single collection of data.
What am I doing wrong?
change it to
$plantLots = PlantLot::with(['controlNumber' => function($q){
$q->groupBy('creator_id');
}])
->whereHas('controlNumber', function ($q) use ($fromDate, $toDate) {
$q->where('created_at', '>=', $fromDate)
->when($toDate != '',function($q){
$q->where('created_at', '=<', $toDate);
});
})->get();
Hope it helps.