pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<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="9x77fdh" 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="f5dxz7f" id='e8Qb4'><tfoot id='e8Qb4'></tfoot><dl id='e8Qb4'><fieldset id='e8Qb4'></fieldset></dl></div>

                本文介紹了使用 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 塊方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                相關文檔推薦

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

                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="bfn3vht" id='PnRfG'><tfoot id='PnRfG'></tfoot><dl id='PnRfG'><fieldset id='PnRfG'></fieldset></dl></div>
                        2. 主站蜘蛛池模板: 尚为传动-专业高精密蜗轮蜗杆,双导程蜗轮蜗杆,蜗轮蜗杆减速机,蜗杆减速机生产厂家 | 喷播机厂家_二手喷播机租赁_水泥浆洒布机-河南青山绿水机电设备有限公司 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | ?水马注水围挡_塑料注水围挡_防撞桶-常州瑞轩水马注水围挡有限公司 | 自动配料系统_称重配料控制系统厂家 | 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | 深圳工程师职称评定条件及流程_深圳职称评审_职称评审-职称网 | elisa试剂盒-PCR试剂盒「上海谷研实业有限公司」 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 | 液氮罐_液氮容器_自增压液氮罐-北京君方科仪科技发展有限公司 | 环球周刊网| 耐酸泵,耐腐蚀真空泵,耐酸真空泵-淄博华舜耐腐蚀真空泵有限公司 精密模具-双色注塑模具加工-深圳铭洋宇通 | 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 明渠式紫外线杀菌器-紫外线消毒器厂家-定州市优威环保 | 东亚液氮罐-液氮生物容器-乐山市东亚机电工贸有限公司 | 臭氧灭菌箱-油桶加热箱-原料桶加热融化烘箱-南京腾阳干燥设备厂 臭氧发生器_臭氧消毒机 - 【同林品牌 实力厂家】 | 石家庄救护车出租_重症转院_跨省跨境医疗转送_活动赛事医疗保障_康复出院_放弃治疗_腾康26年医疗护送转诊团队 | 不锈钢列管式冷凝器,换热器厂家-无锡飞尔诺环境工程有限公司 | 磷酸肌酸二钠盐,肌酐磷酰氯-沾化欣瑞康生物科技 | 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 合肥角钢_合肥槽钢_安徽镀锌管厂家-昆瑟商贸有限公司 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 河南正规膏药生产厂家-膏药贴牌-膏药代加工-修康药业集团官网 | 玻璃钢型材-玻璃钢风管-玻璃钢管道,生产厂家-[江苏欧升玻璃钢制造有限公司] | 不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰]-不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰] | 电采暖锅炉_超低温空气源热泵_空气源热水器-鑫鲁禹电锅炉空气能热泵厂家 | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 万家财经_财经新闻_在线财经资讯网 | 【365公司转让网】公司求购|转让|资质买卖_股权转让交易平台 | 交联度测试仪-湿漏电流测试仪-双85恒温恒湿试验箱-常州市科迈实验仪器有限公司 | 金刚网,金刚网窗纱,不锈钢网,金刚网厂家- 河北萨邦丝网制品有限公司 | 石磨面粉机|石磨面粉机械|石磨面粉机组|石磨面粉成套设备-河南成立粮油机械有限公司 | 金联宇电缆总代理-金联宇集团-广东金联宇电缆实业有限公司 | 中红外QCL激光器-其他连续-半导体连续激光器-筱晓光子 | 化工ERP软件_化工新材料ERP系统_化工新材料MES软件_MES系统-广东顺景软件科技有限公司 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 奥运星-汽车性能网评-提供个性化汽车资讯| 福建自考_福建自学考试网|