php - 在 Laravel 中分页时非法的运算符和值组合
问题描述
我是 Laravel 6 的新手。在视图中,我创建了一个表单,当它提交时,它应该返回一个过滤了底部分页链接的表格。一切正常,但是当我单击分页链接时出现以下错误:“非法运算符和值组合。”
我什至尝试在视图中使用“渲染”方法,但没有任何改变。我看到了链接,但是当我单击其中一个时,会出现错误。
看法
@if (isset($meetings))
<div class="container">
<table class="table table-condensed table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Participants</th>
<th>Description</th>
<th>Room</th>
<th>Date</th>
<th>Start Hour</th>
<th>End Hour</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach($meetings as $meeting)
<tr>
<td>{{ $meeting->id }}</td>
<td>{{ $meeting->id_participants }}</td>
<td>{{ $meeting->description }}</td>
<td>{{ $meeting->id_room }}</td>
<td>{{ $meeting->date }}</td>
<td>{{ $meeting->start_hour }}</td>
<td>{{ $meeting->end_hour }}</td>
<td><a href="{{ route('updateMeeting', ['id' => $meeting->id]) }}" class= "text-center"><button type="button" class="btn btn-primary">Edit</button></a></td>
<td>
<form action="{{ route('nextMeetingsDeleteMeeting', ['id' => $meeting->id]) }}" method="POST">
@csrf
{{ method_field('PUT') }}
<button type="submit" class="btn btn-danger" id={{ $meeting->id }}>Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
<div>
{{ $meetings->links() }}
</div>
</div>
@endif
控制器
public function fetch_table(Request $request)
{
$this->validate($request, [
'start_date' => 'date',
'end_date' => 'date',
]);
$start_date = $request['start_date'];
$end_date = $request['end_date'];
$participants = $request['participants'];
$room_input = $request['rooms'];
$rooms = Room::all();
$users = User::all()->where('is_active', '1')->sortBy('surname');
$meetings = $this->build_table($room_input, $start_date, $end_date, $participants);
return view('reportArea', compact('users', 'rooms', 'meetings', 'start_date', 'end_date', 'participants',
'room_input'))->withInput($request->all());
}
public function build_table($room, $start_date, $end_date, $participants)
{
if (!empty($room) && !empty($participants)) {
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('id_room', $room)
->where('date', '>', $start_date)
->where('date', '<', $end_date)
->where(function ($query) use ($participants) {
$query->where('id_participants', $participants)
->orWhere('id_participants', 'like', '%;'.$participants)
->orWhere('id_participants', 'like', $participants.';%')
->orWhere('id_participants', 'like', '%;'.$participants.';%');
})
->paginate(2);
} elseif (!empty($participants)) {
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('date', '>', $start_date)
->where('date', '<', $end_date)
->where(function ($query) use ($participants) {
$query->where('id_participants', $participants)
->orWhere('id_participants', 'like', '%;'.$participants)
->orWhere('id_participants', 'like', $participants.';%')
->orWhere('id_participants', 'like', '%;'.$participants.';%');
})
->paginate(2);
} elseif (!empty($rooms)) {
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('date', '>', $start_date)
->where('date', '<', $end_date)
->where('id_room', $room)
->paginate(2);
} else {
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('date', '>', $start_date)
->where('date', '<', $end_date)
->paginate(2);
}
return $meetings;
}
路线
Route::get('/reportarea/fetchtable', 'ReportAreaController@fetch_table')->name('reportAreaFetchTable');
目前,一切正常,但是当我单击链接时,会出现上述错误。换句话说,如果我添加方法,paginate(2)
我只能在表格上正确看到两行,但是当我单击链接查看其他行时,它无法正常工作。有人能帮我解决这个问题吗?
解决方案
您可能需要将查询字符串附加到寻呼机的链接以传递您需要的其他参数。您将null
作为这些查询参数的值传递并收到错误。
举个例子:
{{ $meetings->appends(['start_date' => ..., 'end_date' => ..., ...])->links() }}
或者只是传递所有当前查询参数(分页器将忽略当前页面使用的任何键):
{{ $meetings->appends(request()->query())->links() }}
推荐阅读
- android - 如何在 Flutter 中使用 MethodChannel 读取特定文件夹?
- node.js - MongoDB / Geojson $geointersects 问题
- c++ - 我倒数第二个值是垃圾,程序是关于计算总和
- laravel - Laravel 嵌套的 foreach 返回重复项
- angular - Ag Grid 单元格渲染器
- c# - 在 web api 请求中接收布尔参数时如何将 0/1 映射为真/假
- python - 套接字不能在不同设备上的同一网络上工作
- arrays - 如何排除数组列表中不存在的项目?
- amazon-web-services - 如何在 AWS elasticsearch 中通过模板显式放置分片大小?
- amazon-web-services - 告诉 Terraform 忽略不同工作区中的 Route53 资源