Skip to content

A free open source IT asset/license management system

Fixed custom report ambiguous ID query

snipe4136b4eb8eff
diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php
index 0566bfbe39..9e162b28b2 100644
--- a/app/Http/Controllers/ReportsController.php
+++ b/app/Http/Controllers/ReportsController.php
@@ -808,11 +808,22 @@ class ReportsController extends Controller
$checkout_start = Carbon::parse($request->input('checkout_date_start'))->startOfDay();
$checkout_end = Carbon::parse($request->input('checkout_date_end', now()))->endOfDay();
- $actionlogassets = Actionlog::select('item_id')->where('action_type', '=', 'checkout')
- ->where('item_type', '=', Asset::class)
- ->whereBetween('action_date', [$checkout_start, $checkout_end]); // we are *not* doing ->get()...
-
- $assets->whereIn('assets.id', $actionlogassets); // ...because this _should_ act as a 'subquery'
+ // Inline closure rather than a pre-built Eloquent Builder so
+ // the subquery's `select('item_id')` clause is preserved. When
+ // passed an Eloquent Builder as the second whereIn argument,
+ // Laravel doesn't always propagate the SELECT to the subquery
+ // and falls back to `select id`, which is wrong here (we want
+ // action_logs.item_id, not action_logs.id) and additionally
+ // combines with the InCategory scope's models/categories joins
+ // to produce an ambiguous outer `id` in the generated SQL.
+ $assets->whereIn('assets.id', function ($q) use ($checkout_start, $checkout_end) {
+ $q->select('item_id')
+ ->from('action_logs')
+ ->where('action_type', '=', 'checkout')
+ ->where('item_type', '=', Asset::class)
+ ->whereBetween('action_date', [$checkout_start, $checkout_end])
+ ->whereNull('deleted_at');
+ });
}
if (($request->filled('checkin_date_start'))) {