@@ -808,11 +808,22 @@ public function postCustom(CustomAssetReportRequest $request): StreamedResponse
808808 $ checkout_start = Carbon::parse ($ request ->input ('checkout_date_start ' ))->startOfDay ();
809809 $ checkout_end = Carbon::parse ($ request ->input ('checkout_date_end ' , now ()))->endOfDay ();
810810
811- $ actionlogassets = Actionlog::select ('item_id ' )->where ('action_type ' , '= ' , 'checkout ' )
812- ->where ('item_type ' , '= ' , Asset::class)
813- ->whereBetween ('action_date ' , [$ checkout_start , $ checkout_end ]); // we are *not* doing ->get()...
814-
815- $ assets ->whereIn ('assets.id ' , $ actionlogassets ); // ...because this _should_ act as a 'subquery'
811+ // Inline closure rather than a pre-built Eloquent Builder so
812+ // the subquery's `select('item_id')` clause is preserved. When
813+ // passed an Eloquent Builder as the second whereIn argument,
814+ // Laravel doesn't always propagate the SELECT to the subquery
815+ // and falls back to `select id`, which is wrong here (we want
816+ // action_logs.item_id, not action_logs.id) and additionally
817+ // combines with the InCategory scope's models/categories joins
818+ // to produce an ambiguous outer `id` in the generated SQL.
819+ $ assets ->whereIn ('assets.id ' , function ($ q ) use ($ checkout_start , $ checkout_end ) {
820+ $ q ->select ('item_id ' )
821+ ->from ('action_logs ' )
822+ ->where ('action_type ' , '= ' , 'checkout ' )
823+ ->where ('item_type ' , '= ' , Asset::class)
824+ ->whereBetween ('action_date ' , [$ checkout_start , $ checkout_end ])
825+ ->whereNull ('deleted_at ' );
826+ });
816827 }
817828
818829 if (($ request ->filled ('checkin_date_start ' ))) {
0 commit comments