好吧,這讓我發瘋了。有人可以解釋為什么
Result::query()
->where('audit_id', $audit->id)
->where('result_type', 'App\Models\Touchpoint')
->whereIn('result_id', $tps)
->with('result')
->join('locations', function ($join) {
$join->where('locations.id', $location->id);
})
->join('location_weight', function ($join) {
$join->on('results.result_id', '=', 'location_weight.weight_id')
->on('results.result_type', '=', 'location_weight.weight_type')
->on('locations.id', '=', 'location_weight.location_id');
})
->get()
當 $audit->id = 1 和 $location->id = 1 時不回傳結果,但是
Result::query()
->where('audit_id', 1)
->where('result_type', 'App\Models\Touchpoint')
->whereIn('result_id', $tps)
->with('result')
->join('locations', function ($join) {
$join->where('locations.id', 1);
})
->join('location_weight', function ($join) {
$join->on('results.result_id', '=', 'location_weight.weight_id')
->on('results.result_type', '=', 'location_weight.weight_type')
->on('locations.id', '=', 'location_weight.location_id');
})
->get()
是否回傳正確的結果?
uj5u.com熱心網友回復:
第一個代碼塊應該拋出一個錯誤。您從父作用域 ( $location) 參考了一個變數,而沒有將其傳遞給use語言結構
// This
->join('locations', function ($join) {
$join->where('locations.id', $location->id);
})
// should be
->join('locations', function ($join) use ($location) {
$join->where('locations.id', $location->id);
})
- https://www.php.net/manual/en/functions.anonymous.php (參見示例#3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485947.html
上一篇:去掉laravel的認證
