laravel的paginate()分頁,如果用post傳參,點擊第二頁時會默認使用get,就會返回原始數據了
需要把查詢數據get方式也放到paginate()分頁參數中
一、路由
Route::group(['prefix'=>'help'], function (){
Route::get('/pickup/{id}', 'TaskHelpController@pickupList'); //拾金不昧
});
//id是區分1幫助他人2尋求幫助
二、視圖
<div class="ibox-content">
<div class="row">
<div class="col-sm-12">
<div class="ibox float-e-margins">
<div class="ibox-content">
<form id="form_condition" class="form-search" method="get" action="{{ url('admin/help/pickup', ['id' => $is_type]) }}">
<div class="text-c ">
物品名稱:
<select name="pickup_goods" class="form-control inline" id="pickup_goods" style="width:200px;">
{{--1拾金不昧 => 1鑰匙 2錢包 3手機 4證件 5寵物 6其他--}}
<option value="">物品-請選擇</option>
<option value="1">鑰匙</option>
<option value="2">錢包</option>
<option value="3">手機</option>
<option value="4">證件</option>
<option value="5">寵物</option>
<option value="6">其他</option>
</select>
任務狀態:
<select name="status" class="form-control inline" id="status" style="width:200px;">
{{--任務狀態 1:待接單 2:已接單 3:已完成--}}
<option value="">狀態-請選擇</option>
<option value="1">待接單</option>
<option value="2">已接單</option>
<option value="3">已完成</option>
</select>
<input type="text" class="form-control " id="id" style="width:150px;display: inline-block"
placeholder="ID搜索" name="id" value="">
<input type="text" class="form-control " id="phone" style="width:150px;display: inline-block"
placeholder="手機號搜索" name="phone" value="">
{{--<input type="text" class="form-control " id="key_name" style="width:150px;display: inline-block"--}}
{{--placeholder="昵稱搜索" name="key_name" value="">--}}
<input type="text" class="form-control " id="title" style="width:150px;display: inline-block"
placeholder="事情搜索" name="title" value="">
<input type="text" class="form-control " id="content" style="width:150px;display: inline-block"
placeholder="事情經過搜索" name="content" value="">
<button class="btn btn-info " name="" type="submit">
<i class="fa fa-search"></i> 搜索
</button>
<a class="btn btn-default " href="{{ url('admin/help/pickup', ['id' => $is_type]) }}">
<i class="fa fa-times"></i> 清空搜索
</a>
</div>
</form>
</div>
</div>
</div>
</div>
<table class="table table-striped table-bordered table-hover dataTables-example">
<thead>
<tr>
<th>ID</th>
<th>發布人</th>
<th>電話</th>
<th>拾取物品名稱</th>
<th>主圖</th>
<th>附屬圖</th>
<th>事發時間</th>
<th>事發地點</th>
<th>事情</th>
<th>事情經過</th>
<th>佣金</th>
<th>任務狀態</th>
<th>創建時間</th>
<th>更新時間</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach ($pickupList as $p)
<tr class="gradeX">
<td class="center">{{ $p->id }}</td>
<td class="center">{{ $p->userInfo->name }}</td>
<td class="center">{{ $p->phone }}</td>
<td class="center">
@if($p->pickup_goods == 1)
鑰匙
@elseif($p->pickup_goods == 2)
錢包
@elseif($p->pickup_goods == 3)
手機
@elseif($p->pickup_goods == 4)
證件
@elseif($p->pickup_goods == 5)
寵物
@else
其他
@endif
</td>
<td class="center">
@if($p->pic)
<a href="{{ $p->pic }}" target="_blank">
<img src="{{ $p->pic }}" width="30px" height="30px">
</a>
@else
暫無圖片
@endif
</td>
<td class="center">
@if($p['otherPic'])
@foreach ($p['otherPic'] as $i)
<a href="{{ $i['img'] }}" target="_blank">
<img src="{{ $i['img'] }}" width="30px" height="30px">
</a>
@endforeach
@else
暫無圖片
@endif
</td>
<td class="center">{{ $p->time }}</td>
<td class="center">{{ $p->address }}</td>
<td class="center">{{ $p->title }}</td>
{{--<td class="center">{{ str_limit($p->content,50,'...') }}</td>--}}
<td class="center">{{ $p->content }}</td>
<td class="center">{{ $p->commission }}</td>
<td class="center">
@if ($p->status == 3)
<span class="label label-primary">已完成</span>
@elseif ($p->status == 2)
<span class="label label-warning">已接單</span>
@else
<span class="label">待接單</span>
@endif
</td>
<td class="center">{{ $p->created_at }}</td>
<td class="center">{{ $p->updated_at }}</td>
<td class="center">
<button class="btn btn-default btn-sm" onclick="del( {{ $p->id }} );">刪除</button>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="right">
{{ $pickupList->appends($cond)->links() }}
</div>
</div>
{{ $pickupList->appends($cond)->links() }}
//$cond 是查詢參數
三、控制器
/**
* 拾金不昧列表
*/
public function pickupList($id)
{
$condition = [];
$cond = [];
//1鑰匙 2錢包 3手機 4證件 5寵物 6其他
if(!empty($_GET['pickup_goods'])){
array_push($condition, ["pickup_goods", "=", $_GET['pickup_goods']]);
$cond['pickup_goods'] = $_GET['pickup_goods'];
}
//任務狀態 1:待接單 2:已接單 3:已完成
if(!empty($_GET['status'])){
array_push($condition, ["status", "=", $_GET['status']]);
$cond['status'] = $_GET['status'];
}
//keyword
if(!empty($_GET['id'])){
array_push($condition, ["id", "=", $_GET['id']]);
$cond['id'] = $_GET['id'];
}
if(!empty($_GET['phone'])){
array_push($condition, ["phone", "like", "%{$_GET['phone']}%"]);
$cond['phone'] = $_GET['phone'];
}
if(!empty($_GET['title'])){
array_push($condition, ["title", "like", "%{$_GET['title']}%"]);
$cond['title'] = $_GET['title'];
}
if(!empty($_GET['content'])){
array_push($condition, ["content", "like", "%{$_GET['content']}%"]);
$cond['content'] = $_GET['content'];
}
$pickupList = Pickup::with(['userInfo','otherPic'])
->where('Initiator_role', $id)
->where($condition)
->orderBy('id', 'desc')
->paginate(5);
return view('admin.help.pickup_list', ['pickupList' => $pickupList, 'is_type' => $id, 'cond'=>$cond]);
}
'cond'=>$cond //查詢參數也要傳過去
效果:


。
