首页 > 解决方案 > 在 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)我只能在表格上正确看到两行,但是当我单击链接查看其他行时,它无法正常工作。有人能帮我解决这个问题吗?

标签: phplaraveleloquentpaginationlaravel-6

解决方案


您可能需要将查询字符串附加到寻呼机的链接以传递您需要的其他参数。您将null作为这些查询参数的值传递并收到错误。

举个例子:

{{ $meetings->appends(['start_date' => ..., 'end_date' => ..., ...])->links() }}

或者只是传递所有当前查询参数(分页器将忽略当前页面使用的任何键):

{{ $meetings->appends(request()->query())->links() }}

Laravel 6.x 文档 - 分页 - 显示结果 - 附加到分页链接

Laravel 6.x 文档 - 请求 - 检索输入 - 从查询字符串中检索输入


推荐阅读