Laravel5.6---搜索查询 自带paginate()分页 get传参

时间:2022-09-27 20:32:53

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  //查询参数也要传过去

  

效果:

Laravel5.6---搜索查询  自带paginate()分页  get传参

Laravel5.6---搜索查询  自带paginate()分页  get传参