Issue
I'm trying to implement a simple select method into a eloquent with. So, I have this code that works correctly.
$usersInfo = User::with(['product' => function($query) use($request) {
$query->whereBetween('date', [$request->get('datefrom'), $request->get('dateto')]);
}])->get();
But if I put a select into the query function, the query stop working and return an empty value into the product array.
$usersInfo = User::with(['product' => function($query) use($request) {
$query->select(['date', 'name'])
->whereBetween('date', [$request->get('datefrom'), $request->get('dateto')]);
}])->get();
I know that if use DB::table()
is possible to do this stuff.
So, my question is. If there any possibility to put this select into the with, in Eloquent?
Thanks
Solution
You need to pass the primary key of the other model into the select to retrieve the necessary results. Without seeing the product relation function I cannot accurately determine what should be done, however it will probably be id
.
$query->select('id', 'date', 'name')...
Referenced here
Answered By - mbozwood
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.