問題描述
為了處理大型數(shù)據(jù)庫,laravel 提供了 chunk 方法如下 https://laravel.com/docs/5.1/queries#retrieving-results
For processing large database, laravel provides chunk method as here https://laravel.com/docs/5.1/queries#retrieving-results
但是我如何在這個查詢中使用塊方法,
But how can I use chunk method in this query,
$data = Inspector::latest('id')
->select('id', 'firstname', 'status', 'state', 'phone')
->where('firstname', 'LIKE', '%' . $searchtext . '%')
->get();
我在這里返回這樣的 json 響應(yīng),
where I'm returning json response like this,
echo json_encode($data);
任何建議....
推薦答案
據(jù)我所知,chunk()
方法適用于需要處理大型數(shù)據(jù)集并采取對那個數(shù)據(jù)塊一個塊的操作.
As I understand it the chunk()
method is for use when you need to work with a large dataset and take an action on that data chunk by chunk.
從您的問題來看,聽起來您是在執(zhí)行查詢,然后將數(shù)據(jù)作為 JSON 返回,所以對我來說,這聽起來不像是您正在對需要分塊的數(shù)據(jù)集執(zhí)行操作.
From your question, it sounds like you're performing a query then returning the data as JSON so to me, it doesn't sound like you're taking an action on your dataset that requires chunking.
如果您想分解返回的 JSON 數(shù)據(jù),您應(yīng)該查看 分頁.
If you want to break up the returned JSON data you should be instead looking at pagination.
您可以像這樣對查詢應(yīng)用分頁:
You could apply pagination to your query like so:
$data = Inspector::latest('id')
->select('id', 'firstname', 'status', 'state', 'phone')
->where('firstname', 'LIKE', '%' . $searchtext . '%')
->paginate();
您可以通過向 paginate 方法傳遞一個數(shù)字來指定每個集合的大小:
You can specify the size of each set by passing a number to the paginate method:
$data = Inspector::latest('id')
->select('id', 'firstname', 'status', 'state', 'phone')
->where('firstname', 'LIKE', '%' . $searchtext . '%')
->paginate(25);
如果我誤解了,而您確實想要進(jìn)行分塊,我相信您可以執(zhí)行以下操作:
If I've misunderstood and you did actually want to do the chunking, I believe you could do the following:
$data = Inspector::latest('id')
->select('id', 'firstname', 'status', 'state', 'phone')
->where('firstname', 'LIKE', '%' . $searchtext . '%')
->chunk(50, function($inspectors) {
foreach ($inspectors as $inspector) {
// apply some action to the chunked results here
}
});
此外,如果您返回一個 eloquent 對象,它將自動轉(zhuǎn)換為 json,因此據(jù)我所知,您不需要執(zhí)行 json_encode()
.
Also, if you're returning an eloquent object it will be automatically cast to json so you don't need to perform json_encode()
as far as I'm aware.
編輯
如果我完全誤解了你,而你真正想做的是:
If I've completely misunderstood you and what you actually want to do is this:
{ 1000 records } -> this is the result of your query
拆分成這樣:
{
{ 300 records},
{ 300 records},
{ 300 records},
{ 100 records},
}
然后你想要Collection
的塊方法:
$data = Inspector::latest('id')
->select('id', 'firstname', 'status', 'state', 'phone')
->where('firstname', 'LIKE', '%' . $searchtext . '%')
->get() // now we're working with a collection
->chunk(300);
請記住,在獲得查詢結(jié)果之前,您不會使用 Collection
,因此如果您只調(diào)用 chunk()
,它將期待一個回調(diào),這將應(yīng)用到整個數(shù)據(jù)集,因為您正在使用 Eloquent
.
Remember you're not working with a Collection
until you get the result of the query so if you just call chunk()
it will expect a callback, which will be applied to the entire dataset, as you're working with Eloquent
.
有關(guān)Collection
chunk()
方法的進(jìn)一步閱讀,請參閱此處:https://laravel.com/docs/5.3/collections#method-chunk
See for further reading here on the Collection
chunk()
method here: https://laravel.com/docs/5.3/collections#method-chunk
否則...你能提供更多關(guān)于你實際在做什么的背景信息嗎?聽起來你真的需要分頁.你在用 JSON 數(shù)據(jù)做什么,你如何調(diào)用 HTTP 請求,是通過 ajax 嗎?為什么你一次需要全部 1000 條記錄?
Otherwise... Could you give some more context to what you're actually doing? It really sounds like you need pagination. What are you doing with the JSON data and how are you invoking the HTTP request, is it via ajax? Why do you need the whole 1000 records all at once?
如果您確實需要將 1000 個的整個數(shù)據(jù)集發(fā)送到客戶端,但一次發(fā)送 300 個,那么您不想使用塊.考慮一下 chunk()
在 eloquent 上下文中的用途,它不是為了讓塊一次一個塊地返回給客戶端,直到它擁有整個數(shù)據(jù)集 - 它是為了將一個動作應(yīng)用于一個塊然后返回整個集合,使用它的目的是通過加載整個集合來處理操作,一次不會占用太多內(nèi)存.
If you really need the whole dataset of 1000 sent to the client, but 300 at a time then you don't want to use chunk. Think about what chunk()
is for in the context of eloquent, it's not for getting chunks to return to the client a chunk at a time until it has the whole data set - it's for applying an action to a chunk then returning the whole set and the point of using it is so it doesn't take up too much memory at one time by loading the whole set to process the action on.
如果你想一點一點地獲取整個數(shù)據(jù)集,而分頁對你的情況不起作用(我還沒有明白為什么!)你需要多次調(diào)用 HTTP 請求來一點一點地獲取數(shù)據(jù),然后在每個請求中指定您已經(jīng)擁有的和您需要的.
If you want the whole data set bit by bit and pagination won't work for your case (I'm yet to see why!) you will need to invoke the HTTP request several times to get the data bit by bit and specify in each request what you already have and what you need.
這篇關(guān)于使用 Eloquent 的 Laravel 塊方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!