I have this simple eloquent query, works fine, but with few records.
When database increments until 100000 records become very slow.
我读的应该是使用chunk而不是get.我如何为这个查询实现它?
$collection = Contact::with('shop');
$collection = $collection->orderBy('created_at', 'DESC');
$collection = $collection->get();
$json = $collection->map(function ($contact) {
return [
'id' => $contact->id,
'name' => $contact->name,
...about 50 columns more.
'shop' => [
'id' => optional($contact->shop)->id,
'name' => optional($contact->shop)>name
],
...about 6 relations more.
];
});
$json = $json->paginate(50);
return response()->json(['contacts' => $json], 200);