久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='sP3vf'><tr id='sP3vf'><dt id='sP3vf'><q id='sP3vf'><span id='sP3vf'><b id='sP3vf'><form id='sP3vf'><ins id='sP3vf'></ins><ul id='sP3vf'></ul><sub id='sP3vf'></sub></form><legend id='sP3vf'></legend><bdo id='sP3vf'><pre id='sP3vf'><center id='sP3vf'></center></pre></bdo></b><th id='sP3vf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='sP3vf'><tfoot id='sP3vf'></tfoot><dl id='sP3vf'><fieldset id='sP3vf'></fieldset></dl></div>

    <legend id='sP3vf'><style id='sP3vf'><dir id='sP3vf'><q id='sP3vf'></q></dir></style></legend><tfoot id='sP3vf'></tfoot>

    <small id='sP3vf'></small><noframes id='sP3vf'>

      • <bdo id='sP3vf'></bdo><ul id='sP3vf'></ul>

      使用 Eloquent 的 Laravel 塊方法

      Laravel Chunk Method Using Eloquent(使用 Eloquent 的 Laravel 塊方法)
        <legend id='e8Qb4'><style id='e8Qb4'><dir id='e8Qb4'><q id='e8Qb4'></q></dir></style></legend>
        • <bdo id='e8Qb4'></bdo><ul id='e8Qb4'></ul>

            <tbody id='e8Qb4'></tbody>

          <small id='e8Qb4'></small><noframes id='e8Qb4'>

            <tfoot id='e8Qb4'></tfoot>
          1. <i id='e8Qb4'><tr id='e8Qb4'><dt id='e8Qb4'><q id='e8Qb4'><span id='e8Qb4'><b id='e8Qb4'><form id='e8Qb4'><ins id='e8Qb4'></ins><ul id='e8Qb4'></ul><sub id='e8Qb4'></sub></form><legend id='e8Qb4'></legend><bdo id='e8Qb4'><pre id='e8Qb4'><center id='e8Qb4'></center></pre></bdo></b><th id='e8Qb4'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='e8Qb4'><tfoot id='e8Qb4'></tfoot><dl id='e8Qb4'><fieldset id='e8Qb4'></fieldset></dl></div>

                本文介紹了使用 Eloquent 的 Laravel 塊方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                為了處理大型數(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)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標(biāo)不起作用)
                PHP PDO ODBC connection(PHP PDO ODBC 連接)
                Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術(shù)方法)
                php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動程序)

                1. <small id='PnRfG'></small><noframes id='PnRfG'>

                    <bdo id='PnRfG'></bdo><ul id='PnRfG'></ul>

                    <legend id='PnRfG'><style id='PnRfG'><dir id='PnRfG'><q id='PnRfG'></q></dir></style></legend>

                      <tfoot id='PnRfG'></tfoot>
                          <tbody id='PnRfG'></tbody>
                        1. <i id='PnRfG'><tr id='PnRfG'><dt id='PnRfG'><q id='PnRfG'><span id='PnRfG'><b id='PnRfG'><form id='PnRfG'><ins id='PnRfG'></ins><ul id='PnRfG'></ul><sub id='PnRfG'></sub></form><legend id='PnRfG'></legend><bdo id='PnRfG'><pre id='PnRfG'><center id='PnRfG'></center></pre></bdo></b><th id='PnRfG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='PnRfG'><tfoot id='PnRfG'></tfoot><dl id='PnRfG'><fieldset id='PnRfG'></fieldset></dl></div>
                        2. 主站蜘蛛池模板: 日本精品一区二区 | 电影午夜精品一区二区三区 | 日本三级电影免费 | 精品视频网| 国产真实精品久久二三区 | 国产麻豆一区二区三区 | www.国产 | 欧美日本一区 | 欧美黄色一区 | 婷婷在线网站 | 天天操综合网站 | 亚洲毛片在线观看 | 国产精品五区 | 精品亚洲视频在线 | 91直接看| 91视频a| 在线观看国产精品视频 | 国产一级特黄视频 | 夜夜夜操| 欧洲一级黄 | 黄色网址在线播放 | 欧美video| 婷婷综合色 | 999精品在线 | 国产激情视频在线观看 | 亚洲精品久久久蜜桃网站 | 日韩免费1区二区电影 | 欧美成视频 | 国产91成人 | 熟女毛片| 久久久久久久久久久久久九 | 超碰欧美 | 亚洲人久久 | 动漫www.被爆羞羞av44 | 欧美一级片在线观看 | 色精品 | 国产成人精品综合 | 亚洲精品久 | 欧美一区视频 | 中文字幕视频在线 | 久久久性色精品国产免费观看 |