Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shone83's avatar

On checkbox update this error: Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id

I have a table with checkbox where I only need to update just that checkbox, where default value is 0 and boolean in migration. CallCenterController look like this:

public function edit($id)
{
    $callCenter = AddMember::findOrFail($id);

    return view('callcenter.index', compact('callCenter'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $callCenter = AddMember::findOrFail($id);

    $callCenter->update($request->all());

    return redirect()->back();
}

and view like this:

{!! Form::model($callCenter, ['method'=>'PATCH', 'action'=>['CallCenterController@update', $callCenter->id]]) !!}

<div class="table-responsive">
        <table class="table table-hover">
        <thead>
            <tr>
                <th>Name</th>
                <th>Last Name</th>
                <th>Confirmed</th>
            </tr>
        </thead>
        <tbody>

        @if ($callCenter)

            @foreach ($callCenter as $call)

                <tr @if ($call->confirmed) class="success" @endif>
                    <td>{{$call->name}}</td>
                    <td>{{$call->last_name}}</td>
                    <td><input type="checkbox" name="confirmed" @if ($call->confirmed) checked=checked @endif></td>
                </tr>

            @endforeach

        @endif

        </tbody>
        </table>
    </div>

    <div class="form-group">
        {!! Form::submit('Confirm', ['class'=>'btn btn-primary pull-right']) !!}
    </div>

    {!! Form::close() !!}

but I have this error:

(2/2) ErrorException Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id

when I try to enter that page... I already try without pagination and same error appear so it's not that. Any idea?

0 likes
34 replies
Snapey's avatar

Do you get the error when you SHOW the form or when you POST the form ?

shone83's avatar

When I show the form, now I can't see table...

Snapey's avatar

So can we see the relevant part of the controller?

Also, what line is the error on?

shone83's avatar

How do you mean relevant? I first made index method where I get data from table with checkbox and simple search. That works fine, I can show you if you need. But, only when I try to make checkbox to be updated I get this error, that's way I show only edit and update methods in controller.

Error is in line where table starts.

Cronix's avatar

findOrFail returns a single result, not a collection, but you're looping over $callCenter as if it were a collection of many results in the view.

Snapey's avatar

Well you say

I already try without pagination and same error appear so it's not that.

But the code you show does not include pagination. Are you sure then that the code you show is actually being run in this case?

You are getting error with paginated data, plus you are trying to loop over a collection when the controller only loads one value.

Everything points to the problem being with code you are not showing, or you are incorrectly routing to the wrong controller method

shone83's avatar

@Snapey

Ok, this is entirely CallCenterController, route and view:

CallCenterController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\AddMember;

class CallCenterController extends Controller
{
    public function index(Request $request)
    {
        $callCenter = AddMember::where(function ($query) use ($request) {
            if (($term = $request->get('term'))) {
              $query->where('name', 'like', '%' . $term . '%');
              $query->orWhere('last_name', 'like', '%' . $term . '%');
              $query->orWhere('street', 'like', '%' . $term . '%');
              $query->orWhere('social_number', 'like', '%' . $term . '%');
              $query->orWhere('telephone_1', 'like', '%' . $term . '%');
              $query->orWhere('telephone_2', 'like', '%' . $term . '%');
              $query->orWhere('email', 'like', '%' . $term . '%');
            }
          })
          ->where('town_id', Auth::user()->town_id)
          ->orderBy('last_name')
          ->paginate(10);

        return view('callcenter.index', compact('callCenter'));
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        //
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $callCenter = AddMember::findOrFail($id);

        return view('callcenter.index', compact('callCenter'));
    }

    public function update(Request $request, $id)
    {
        $callCenter = AddMember::findOrFail($id);

        $callCenter->update($request->all());

        return redirect()->back();
    }

route:

Route::resource('/callcenter', 'CallCenterController');

view:

@extends('layouts.app')

@section('content')

    <div class="container">

            {!! Form::open(['route'=>'callcenter.index', 'method'=>'GET']) !!}
                <div class="input-group">
                  {!! Form::text('term', Request::get('term'), ['class'=>'form-control', 'placeholder'=>'Search...']) !!}
                  <div class="input-group-btn">
                    {!! Form::button('<i class="fa fa-search"></i>', ['type' => 'submit', 'class'=>'btn btn-default']) !!}
                  </div>
                </div>
            {!! Form::close() !!}

            {!! Form::model($callCenter, ['method'=>'PATCH', 'action'=>['CallCenterController@update', $callCenter->id]]) !!}

            <div class="table-responsive">
                    <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Last Name</th>
                            <th>Confirmed</th>
                        </tr>
                    </thead>
                    <tbody>
            
                    @if ($callCenter)
            
                        @foreach ($callCenter as $call)
            
                            <tr @if ($call->confirmed) class="success" @endif>
                                <td>{{$call->name}}</td>
                                <td>{{$call->last_name}}</td>
                                <td><input type="checkbox" name="confirmed" @if ($call->confirmed) checked=checked @endif></td>
                            </tr>
            
                        @endforeach
            
                    @endif
            
                    </tbody>
                    </table>
                </div>

                <div class="form-group">
                    {!! Form::submit('Confirm', ['class'=>'btn btn-primary pull-right']) !!}
                </div>

                {!! Form::close() !!}
            
                <div class="row">
                    <div class="col-sm-6 col-sm-offset-5">
                        {{$callCenter->links()}}
                    </div>
                </div>

    </div>

@endsection

hope that now it's more clear what I trying to do.

@Cronix

You are right. I'm trying to update a more then one result at the time. As you can see above, I have table with checkboxes and submit button but when I try to update error appear.

Snapey's avatar

When you click on your form, you are accessing this route

{!! Form::open(['route'=>'callcenter.index', 'method'=>'GET']) !!}

callcenter.index when you should be calling callcenter.edit (according to your earlier posts)

Snapey's avatar

Sorry, scrub that. Looking again and trying to workout what you are doing

Not sure where the edit method enters into it...

Snapey's avatar

change the return

    public function update(Request $request, $id)
    {
        $callCenter = AddMember::findOrFail($id);

        $callCenter->update($request->all());

        return redirect(route('callcenter.index'));
    }
shone83's avatar

I know what you saying but I can't figure out how to edit on same page where index are? I get same error with route('callcenter.index') in redirect.

Snapey's avatar

You cannot do this

            {!! Form::model($callCenter, ['method'=>'PATCH', 'action'=>['CallCenterController@update', $callCenter->id]]) !!}

when $callCenter is a paginated collection and not a single model.

You need to not model bind the form, but instead give each checkbox the ID of the callcenter you want to confirm, then in the update method, loop over all the checkboxes and see which ones need updating.

Alternatively, list the call centres and have a button next to each and confirm them one by one.

shone83's avatar

You need to not model bind the form, but instead give each checkbox the ID of the callcenter you want to confirm, then in the update method, loop over all the checkboxes and see which ones need updating.

Do you have some example of this that I can see how is done? Some link?

Alternatively, list the call centres and have a button next to each and confirm them one by one.

I tried like this but same error.

Only when I put this:

{!! Form::model($callCenter, ['method'=>'PATCH', 'action'=>['CallCenterController@update', $callCenter->id]]) !!}

{!! Form::close() !!}

I get Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id error...

Cronix's avatar

You also said you removed the paginator, but still have {{$callCenter->links()}} in the view

Cronix's avatar

And in the edit method, you have

$callCenter = AddMember::findOrFail($id);

which returns one result...

but in the view you are looping over it like it's many results...

@foreach ($callCenter as $call)
            
                            <tr @if ($call->confirmed) class="success" @endif>
                                <td>{{$call->name}}</td>
                                <td>{{$call->last_name}}</td>
                                <td><input type="checkbox" name="confirmed" @if ($call->confirmed) checked=checked @endif></td>
                            </tr>
            
                        @endforeach 

There isn't anything to loop over. So you don't need that foreach loop at all

shone83's avatar

@Cronix

I did remove pagination to see if that couse the error, after removin error is still there so I put it back and when you ask me to put entire code I copy with pagination.

I need foreach loop for table. About findOrFail which return one result, what's the alternative?

This is how it looks if I remove form:

https://ibb.co/jH8RFJ

Snapey's avatar

@cronix the edit form is a complete redherring as far as this question goes.

The OP is passing a collection into the index view and then trying to create a form that allows any row to be confirmed. The issue is that the form action is trying to get the ID from a paginated collection. It does not make sense to use ID in this context, hence I suggested options of a form that processes many checkboxes or one form per row of the table.

Snapey's avatar
Snapey
Best Answer
Level 122

Switch the form for this;

<form action="{{ url('CallCenterController@confirm') }}" method="post">
    {{ csrf_field() }}
    <div class="table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Last Name</th>
                    <th>Confirmed</th>
                </tr>
            </thead>
            <tbody>

            @if ($callCenter)

                @foreach ($callCenter as $call)

                    <tr @if ($call->confirmed) class="success" @endif>
                        <td>{{$call->name}}</td>
                        <td>{{$call->last_name}}</td>
                        <td>
                            <input type="hidden" value="0" name="confirmed[{{ $call->id }}]" />
                            <input type="checkbox" name="confirmed[{{ $call->id }}]" @if ($call->confirmed) checked=checked @endif />
                        </td>
                    </tr>

                @endforeach

            @endif

            </tbody>
        </table>
    </div>

    <div class="form-group">
        {!! Form::submit('Confirm', ['class'=>'btn btn-primary pull-right']) !!}
    </div>

    </form>

Then you have a form which contains a confirmed field for each $call

create a new route to a new method in the controller

Route::post('name your route here', 'CallCentreController@confirm');

/**
 * Update the confirmed
 */
public function confirm(Request $request)
{
    foreach($request->confirmed as $key => $value) {

        AddMember::update($key, $value);
    }

    return redirect(route('callcenter.index'));
}

1 like
shone83's avatar

@Snapey

This error apear:

 (1/1) ErrorException

Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically

p.s. I edit:

<form action="{{ url('CallCenterController@confirm') }}" method="post">

to:

<form action="{{ url('/callcenter') }}" method="post">

because in first form it redirect me to CallCenterController@confirm url.

Snapey's avatar

Sorry, my error

/**
 * Update the confirmed
 */
public function confirm(Request $request)
{
    foreach($request->confirmed as $key => $value) {

        AddMember::find($key)->update('confirmed' => $value);

    }

    return redirect(route('callcenter.index'));
}
1 like
shone83's avatar

First in this line:

AddMember::find($key)->update('confirmed' => $value);

error on index:

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')'

when I change to:

AddMember::find($key)->update('confirmed', $value);

I get this error when I confirm:

Type error: Argument 1 passed to Illuminate\Database\Eloquent\Model::update() must be of the type array, string given, called in C:\xampp\htdocs\kapCMS3\app\Http\Controllers\CallCenterController.php on line 44

Cronix's avatar

Yes, it needs to be an array of key/value pairs to update. You're just missing the [ ] brackets

AddMember::find($key)->update(['confirmed' => $value]);
1 like
shone83's avatar

Now this:

(2/2) QueryException

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'on' for column 'confirmed' at row 1 (SQL: update add_members set confirmed = on, updated_at = 2018-06-12 23:24:17 where id = 6)

:)

Cronix's avatar

You should be able to troubleshoot some of this stuff. It's telling you exactly what's wrong. If you look in the query for the value it's trying to insert for confirmed, it's on. Mysql is saying the column type is an integer, so on isn't an int, right? The reason it's sending "on" is because you didn't give your checkbox a value here

<input type="checkbox" name="confirmed[{{ $call->id }}]" @if ($call->confirmed) checked=checked @endif />

so add value="1" to it, like you did here for if they aren't confirmed

<input type="hidden" value="0" name="confirmed[{{ $call->id }}]" />
1 like
shone83's avatar

In migrations confirmed column is boolean and set default to 0:

$table->boolean('confirmed')->default('0');

I don't understand you where I'm suppose to add value="1" ?

Cronix's avatar

As I said, just like you did on the other one.

<input type="checkbox" value="1" name="confirmed[{{ $call->id }}]" @if ($call->confirmed) checked=checked @endif />

So if it's checked, it will send 1 instead of the default on, which is sent if you don't set a value attribute. This is basic HTML.

1 like
shone83's avatar

That's it, it's working now :) I must analyse you code because I didn't understand a lot of it. This is first time I did something like this but I thought it would be a lot easier :)

Thanks @Snapey and @Cronix ;)

Cronix's avatar

You're welcome. You should select @snapey post as the solution since he's really the one who really helped.

jlrdw's avatar

Yeah Thanks @Snapey and @Cronix I am stuffed now, 2 bags of popcorn and 2 soda's.

So much easier for me something like:

<input type="checkbox" name="adopted" id="adopted" value="1"<?php echo ($cat->adopted == 1 ? ' checked' : ''); ?>>
Cronix's avatar

@jlrdw you sure eat a lot of crap lol. You're always saying you're eating popcorn lol.

Next

Please or to participate in this conversation.