I currently found out that you can hydrate an Raw sql query.
I have following query:
DB::table(DB::raw('(SELECT *, Y(location) AS longitude, X(location) AS latitude FROM meetings WHERE MBRCONTAINS(@quadrat, location)) AS sub'))
->select(DB::raw('(FLOOR(SQRT(POW((@ibk_breite - sub.latitude) * 111, 2) + POW((@ibk_laenge - sub.longitude) * 111 * ABS(COS(RADIANS(@ibk_breite))),2)))) AS distance, sub.*, latitude, longitude'));
which I hydrate as following
$meetings = Meeting::fromQuery($query->toSql());
In the blade view i need to get some additional data from different tables, for example:
$meeting->user
which references to the User Model. But if I'm not complety wrong that would result to a n+1 problem in a for each loop, because I'm not eager loading it?! So is it possible to eager load the required models as you would normally do with
->with('user', 'books', 'etc...')
??
Also is it possible to paginate it like $meetings = $query->paginate(5);
and do $meetings->withPath('home');
EDIT: Found a solution:
// Do your query stuff
// Get count before the query because it won't work with skip and take parameter
$count = $query->count();
$query->skip($skip);
$query->take($meetingsPerPage);
$meetings = Meeting::fromQuery($query->toSql());
$meetings->load('user', 'interest.image', 'img_thumbnail');
$meetings = new LengthAwarePaginator($meetings, $count, $meetingsPerPage);
$meetings->load
acts as ->with()
.
As last step you need to create a paginator. IMPORTANT: Use query->count()
before you set skip()
and/or take()
Otherwise it will not work.
Original answer from laracasts. Theres also another possibily stated that didn't work for me.
My solution:
// Do your query stuff
// Get count before the query because it won't work with skip and take parameter
$count = $query->count();
$query->skip($skip);
$query->take($meetingsPerPage);
$meetings = Meeting::fromQuery($query->toSql());
$meetings->load('user', 'interest.image', 'img_thumbnail');
$meetings = new LengthAwarePaginator($meetings, $count, $meetingsPerPage);
$meetings->load
acts as ->with()
. As last step you need to create a paginator. IMPORTANT: Use query->count()
before you set skip()
and/or take()
Otherwise it will not work.
Original answer from laracasts. Theres also another possibily stated that didn't work for me.
You can use setCollection() function with LengthAwarePaginator object (found in Illuminate/Pagination/AbstractPaginator.php). Use load() for eager loading.
$users = DB::table('users')->paginate();
$users->setCollection(User::hydrate($users->items())->load(['category']));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With