Laravel hydrateRaw / fromQuery and impatient pagination downloads

Currently, I have learned that you can dampen Raw sql query.

I have the 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 by 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 refers to the user model. But if I am not mistaken, this will lead to n + 1 problem in each cycle, because I do not want to load it ?! So is it possible to load the required models, as usual, using

->with('user', 'books', 'etc...')

??

It is also possible to paginate it as $meetings = $query->paginate(5);well as do$meetings->withPath('home');

EDIT: Found 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 ->with(). . : query->count() skip() / take() .

laracasts. , .

+4

Source: https://habr.com/ru/post/1680232/


All Articles