I have this snippet from the Laravel documentation:
DB::table('users')
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
I need to understand two things.
- Where does the
$query
parameter to the closure come from? I suspect that there is something happening under the hood that I don't understand. The function takes 1 parameter, the$query
, but where does it come from, how does this function know what is in this parameter, what is passed into the function? - It appears that this closure doesn't return a value, there is no
return
statement. So how does thewhereExists
method know the return value of the closure?