使用 Eloquent 的 Laravel 块方法 [英] Laravel Chunk Method Using Eloquent

查看:23
本文介绍了使用 Eloquent 的 Laravel 块方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了处理大型数据库,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 响应,

where I'm returning json response like this,

echo json_encode($data);

任何建议....

推荐答案

据我所知,chunk() 方法适用于需要处理大型数据集并采取对那个数据块一个块的操作.

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.

从您的问题来看,听起来您是在执行查询,然后将数据作为 JSON 返回,所以对我来说,这听起来不像是您正在对需要分块的数据集执行操作.

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 数据,您应该查看 分页.

If you want to break up the returned JSON data you should be instead looking at pagination.

您可以像这样对查询应用分页:

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 方法传递一个数字来指定每个集合的大小:

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);

如果我误解了,而您确实想要进行分块,我相信您可以执行以下操作:

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 对象,它将自动转换为 json,因此据我所知,您不需要执行 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);

请记住,在获得查询结果之前,您不会使用 Collection,因此如果您只调用 chunk(),它将期待一个回调,这将应用到整个数据集,因为您正在使用 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.

有关Collection chunk() 方法的进一步阅读,请参阅此处: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

否则...你能提供更多关于你实际在做什么的背景信息吗?听起来你真的需要分页.你在用 JSON 数据做什么,你如何调用 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 个的整个数据集发送到客户端,但一次发送 300 个,那么您不想使用块.考虑一下 chunk() 在 eloquent 上下文中的用途,它不是为了让块一次一个块地返回给客户端,直到它拥有整个数据集 - 它是为了将一个动作应用于一个块然后返回整个集合,使用它的目的是通过加载整个集合来处理操作,一次不会占用太多内存.

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.

如果你想一点一点地获取整个数据集,而分页对你的情况不起作用(我还没有明白为什么!)你需要多次调用 HTTP 请求来一点一点地获取数据,然后在每个请求中指定您已经拥有的和您需要的.

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.

这篇关于使用 Eloquent 的 Laravel 块方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆