I have issue with taking data from database based on hasMany relations and send it to api in Larave. Below code, Laravel do not give me product of Customer where Customer id = $id. I don't know why, I can't send $id next to $query. If I write number in '' I have good answear but I need take it automaticly.
$products = Product::where('name', 'like', '%'.$request->q.'%')->orWhere('symbol', 'like', '%'.$request->q.'%')->with(['customers' => function ($query, $id) {
$query->where('id', '=', $id);
}])->get();
all api code
Route::get('/customer/{id}/products', function(Request $request, $id){
$customer = Customer::findOrFail($id);
$products = Product::where('name', 'like', '%'.$request->q.'%')->orWhere('symbol', 'like', '%'.$request->q.'%')->with(['customers' => function ($query, $id) {
$query->where('id', '=', $id);
}])->get();
return $products;
});
Thank you in advance.