Laravel pagination memory size error

When I try to select a paginated connection request field, it shows an en error, for example

Allowed memory size of 134217728 bytes exhausted (tried to allocate 45797376 bytes)

My request

$price_all = DB::table('model_price')
            ->join('operator_model','model_price.model_id','=','operator_model.id')
            ->join('operator_route','operator_model.operator_route_id','=','operator_route.id')
            ->join('route', 'operator_route.route_id', '=', 'route.id')
            ->join('operator', 'operator_route.operator_id', '=', 'operator.id')
            ->select('model_price.id', 'model_price.price', 'route.route_name', 'operator.operator_name')
            ->paginate(2);

My database contains only 5 records. this is not big data.

when I try without pagination, then it works fine. as

$price_all = DB::table('model_price')
                ->join('operator_model','model_price.model_id','=','operator_model.id')
                ->join('operator_route','operator_model.operator_route_id','=','operator_route.id')
                ->join('route', 'operator_route.route_id', '=', 'route.id')
                ->join('operator', 'operator_route.operator_id', '=', 'operator.id')
                ->select('model_price.id', 'model_price.price', 'route.route_name', 'operator.operator_name')
                ->get();

Now, how can I optimize this query.

+4
source share
1 answer

Try using Offset and Limit instead of paginate.

$price_all = DB::table('model_price')
               ->join('operator_model','model_price.model_id','=','operator_model.id')
               ->join('operator_route','operator_model.operator_route_id','=','operator_route.id')
               ->join('route', 'operator_route.route_id', '=', 'route.id')
               ->join('operator', 'operator_route.operator_id', '=', 'operator.id')
               ->select('model_price.id', 'model_price.price', 'route.route_name', 'operator.operator_name')
               ->skip(0)
               ->take(2)
               ->get();
+3
source

All Articles