2017-06-14 39 views
0

我剛開始學習laravel。出現了問題,我從包含5000個記錄的數據庫中進行選擇。Laravel分頁如何附加長字符串

我的數據是這樣的 380673113516 ... 和幾個一千多

我打破他們到一個數組

$numberList = explode("\r\n", $request['inputList']); 

而我就與分頁選擇

$objects = Number::has('object')->with('object')->whereIn('number', $numberList)->Paginate(1000); 

若要分頁,我重新發送我的表格

{{ $objects->appends(['inputList' => $_REQUEST['inputList']])->links() }} 

結果的數據我有這樣的鏈接 http://telbase.dev/list?inputList=380673113513%0D%0A380673113514%0D%0A380673113515%0D%0A380673113516%0D%0A380673113517%0D%0A380673113518%0D%0A380673113519%0D%0A380673113520%0D%0A380673113521%0D%0A380673113522%0D%0A380673113523%0D%0A380673113524%0D%0A380673113525%0D%0A380673113526%0D%0A380673113527%0D%0A380673113528%0D%0A380673113529%0D%0A380673113530%0D%0.................&page=2

如果有大量的數據,該字符串是很長,服務器返回一個錯誤414請求URI太大。我認爲我最初做錯了什麼。如何在分頁中正確傳遞如此大的請求。

ps對不起,我使用谷歌翻譯)

+0

你爲什麼要傳送網址中的數據?我無法真正理解你想要做什麼,但是對於分頁你只需要PAGE參數 –

回答

0

web.php:

Route::post('/'data, '<controllername>@<functionname>'); 

控制器:

public function <functionname>(){ 
    //logic to create data 
    $data = response()->json([ 
     'data' => $data 
    ]) 
    return View::make('<view_name>', array('data' => $data)); 
} 

,那麼你應該能夠訪問它在您的觀點:

{{$data[0]-><attribute>}} 

或做foreach語句.. 。

+0

我正在做的類似的東西,我正在建設與幾千條記錄沒有任何問題。 –

+0

但如何分頁這個數據 – Aleksey

+0

看看這裏有什麼幫助。 https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/42293157/how-to-use-pagination-along-with-laravel-eloquent-find-方法合laravel-5-4&VED = 0ahUKEwj3g_-hjr3UAhVELmMKHcX-DKgQFgghMAI&USG = AFQjCNGejT6T7mBPh5eTIjWOE29WlgTIXA&SIG2 = Bdbwr5j-mRPNZvzDyWiuvw –

0

您是否使用POST方法?如果不是的話,你應該因爲獲取請求的長度限制在發佈到服務器配置的地方。

+0

我使用post從textarea獲取數據。但據我所知,爲了工作分頁,我需要重新將它們傳送到網址 – Aleksey

+0

我認爲你會對數據進行查看。像返回路線:: post('/'數據,'<控制器名稱> @<函數名稱') –

0

web.php

Route::get('/test', [ 
    'uses' => '[email protected]', 
    'as'=> 'test' 
]); 

Route::post('/test', [ 
    'uses' => '[email protected]', 
    'as'=> 'test' 
]); 

控制器

public function test(Request $request) 
{ 
     $numberList = explode("\r\n", $request['inputList']); 
     $objects = Number::has('object')->with('object')->whereIn('number', $numberList)->Paginate(10); 
     return view('objects.test')->with('objects', $objects); 
} 

視圖

<form class="form-horizontal" action="{{ route('test') }}" method="POST"> 
    <textarea class="form-control" name="inputList" id="inputList"></textarea> 
    <button type="submit" class="btn btn-default">go</button> 
    <input type="text" name="_token" value="{{ csrf_token() }}" hidden> 
</form> 

     @if ($objects->count() > 0) 
      @foreach($objects as $object) 
       <tr> 
        <td>{{ $object->number }}</td> 
        <td>{{ $object->object->fio }}</td> 
       </tr> 
      @endforeach 
      {{ $objects->links() }} 
     @endif 

當我使形式的請求,其結果是正常顯示。 但是當我嘗試去第2頁時,我得到了一個空白頁面。 所有,因爲當你去到第2頁$ request ['inputList']已經是空的,因此$對象也是空的。 這是爲什麼?