My controller has the 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();
In both approaches, only the 1st request condition is read.
I want to make a suggestion of 1 where() and 1 orderBy to filter in the loaded data.
Is there something I missed? Can I put it right?
source share