In my controller I have following code:
//Example Data;
$date = Carbon::now();
$order = 'name'; // it can be by name, id or created_at;
// Approach i try for getting data of user with eager loaded products
//1st approach
$user = User::with([
'products' => function ($query) use ($date, $order) {
$query->where('created_at', $date)->orderBy($order, 'desc');
},
])->get();
//2nd approach
$user = User::with([
'products' => function ($query) use ($date, $order) {
$query->where('created_at', $date);
$query->orderBy($order, 'desc');
},
])->get();
On both approach, only the 1st condition of the query is being read.
I want to make 1 where()
clause and 1 orderBy
to be filtered in the eager loaded data.
Is there something i miss out doing? Do I code it wrong?