雄辩的Laravel块方法 [英] Laravel Chunk Method Using Eloquent

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

问题描述

为了处理大型数据库,laravel在此处提供了块方法 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();

您可以通过将数字传递给分页方法来指定每个集合的大小:

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

此外,如果您要返回一个雄辩的对象,它将自动转换为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的块方法:

Then you want the Collection's chunk method:

$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数据,以及如何通过ajax调用HTTP请求?为什么一次需要全部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()在雄辩的上下文中的作用,不是让块一次将一块返回给客户端,直到它拥有整个数据集–而是将操作应用于块然后返回整个集合而且使用它的目的是,通过加载整个集合来处理动作,一次不会占用太多内存.

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.

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

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