Illegal operator and value combination when paginating in Laravel

Tags: , , , ,



I am a newbie at Laravel 6. In the view, I created a form, and when it’s submitted, it should return a table filtered with the links of pagination at the bottom. All works correctly, but when I click a link of the pagination appears the following error: “Illegal operator and value combination.”

I tried even using the “render” method in the view, but nothing changed. I see the links, but when I click one of them, the error appears.

View

@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

Controller

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

Route::get('/reportarea/fetchtable', 'ReportAreaController@fetch_table')->name('reportAreaFetchTable');

Currently, all works OK, but when I click a link, the mentioned error appears. In other words, if I add the method paginate(2) I see correctly only two rows on the table, but when I click the link to see the other ones, it doesn’t work correctly. Is anyone able to help me solve this problem?

Answer

You will probably need to be appending to the query string for the links for the pager to pass your other parameters you need. You are passing null as a value for those query parameters and getting the error.

As an example:

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

Or just passing all the current query parameters (the paginator will ignore what ever key is used for the current page):

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

Laravel 6.x Docs – Pagination – Displaying Results – Appending To Pagination Links

Laravel 6.x Docs – Requests – Retrieving Input – Retrieving Input From The Query String



Source: stackoverflow