D2RTECH
Add a query with a join
Just for reference for others, this was solved using below
Select::make('laptop_id')
->label('Laptop')
->options(
Laptop::query()
->leftJoin('laptop_assignments as la', function ($join) {
$join->on('laptops.asset_id', '=', 'la.laptop_id')
->whereRaw('la.assigned_at = (SELECT MAX(assigned_at) FROM laptop_assignments WHERE laptop_id = laptops.asset_id)');
})
->whereNull('la.laptop_id')
->orWhere(function ($query) {
$query->whereNotNull('la.returned_at')
->where('la.returned_at', '<', today());
})
->pluck('asset_id', 'asset_id')
->toArray()
)
->searchable(),
->searchable(),
6 replies
Add a query with a join
technically this is the query which will fetch me the result, how do i include it in options
SELECT laptops.*
FROM laptops
LEFT JOIN (
SELECT laptop_id, MAX(assigned_at) AS latest_assignment
FROM laptop_assignments
GROUP BY laptop_id
) AS latest_assignment ON laptops.asset_id = latest_assignment.asset_id
LEFT JOIN laptop_assignments AS la ON la.asset_id = laptops.asset_id AND la.assigned_at = latest_assignment.latest_assignment
WHERE la.asset_id IS NULL
OR (la.returned_at IS NOT NULL AND la.returned_at < CURDATE());
6 replies
issue with fillrecordusing
It is very surprising that this code did not worked on my Local machine, but when i hosted it on server, it worked absolutely fine.
here is the code snippet
ImportColumn::make('item_loaned')
->requiredMapping()
->fillRecordUsing(function (Inventory $record, string $state): void {
if(strtolower($state) === 'yes')
{
$record->item_loaned = 'Yes';
}
elseif (strtolower($state) === 'no') {
$record->item_loaned = 'No';
}
else {
$record->item_loaned = 'No';
}
})
->rules(['required', 'max:255']),
3 replies