2017-05-09 54 views
2

我嘗試在我的laravel項目中創建搜索表單。允許內存大小134217728字節用盡(試圖分配74735240字節)

所以我想所有的幾乎所有表列傳遞給視圖這樣我就可以這樣我就可以使用它們來創建保管箱的複選框等

但是當我使用它$var = Model::all();返回此錯誤:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 74735240 bytes)

你能否告訴我另一種解決方案?

這是我的控制器:

<?php 

namespace App\Http\Controllers; 

use App\Http\Requests; 
use Illuminate\Http\Request; 
use App\Lot; 

class SearchController extends Controller 
{ 
    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('auth'); 
    } 

    /** 
    * Show the application dashboard. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     $lots = Lot::all(); 
     return view('lots.search')->withLot($lots); 
    } 
} 

謝謝!

回答

1

有太多數據不適合內存。你應該分頁你的結果集。

https://laravel.com/docs/5.4/pagination#basic-usage

public function index() 
{ 
    $lots = DB::table('lots')->paginate(15); 

    return view('lots.search', ['lots' => $lots]); 
} 

或者你也可以增加內存的限制,但是這不是最好的解決辦法。

ini_set('memory_limit','160M'); 
相關問題