0

OK, this is driving me insane. Can someone please explain why

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()

doesn't return results when $audit->id = 1 and $location->id = 1, yet

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()

does return the correct results?

1 Answer 1

1

The first codeblock should throw an error. You're referencing a variable from the parent scope ($location) without passing it to the use language construct

// 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);
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.