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

Shiva's avatar

Call to a member function update() on a non-object

I keep getting this error when I try to update my users

Call to a member function update() on a non-object

Here is my edit.blade.php

@extends('templates::admin')

@section('content')

{{ Form::model($users, array('method' => 'PATCH', 'route' =>'admin.users.update', 'route_id'=>$users->id)) }}
    <ul>
        <li>
            {{ Form::label('name', 'Name') }}
            {{ Form::text('name', $users->name, array('id' => 'name')) }}
        </li>
        <li style="display:none;">
            {{ Form::label('password', 'Password') }}
            {{ Form::text('password', $users->password, array('id' => 'password')) }}
        </li>
        <li>
            {{ Form::label('email', 'Email') }}
            {{ Form::text('email', $users->email, array('id' => 'email')) }}
        </li>
        <li>
            {{ Form::submit('Submit', array('class' => 'btn btn-default', 'role' => 'button')) }}
        </li>
    </ul>
{{ Form::close() }}

@if($errors->any())
    <ul>
        {{ implode('', $errors->all('<li class="error">:message</li>')) }}
    </ul>
@endif

@stop

and here is my UsersController

public function update($id)
    {
        $input = Input::all();
        $validation = Validator::make($input, User::$rules);

        if($validation->fails()){
            return Redirect::route('admin.users.edit')
                ->withInput()
                ->withErrors($validation)
                ->with('message', 'There were validation errors');
        }

        if($validation->passes()){
            $input['password'] = Hash::make($input['password']);
            $users = User::find($id);

            $users->update($input);
            return View::make('users::index' , $id);

            return Redirect::route('admin.users.edit', $id)
                ->withInput()
                ->withErrors($validation)
                ->with('message', 'There were validation errors');
        }
    }

and this is the line that is giving problems

$users->update($input);
0 likes
28 replies
seb7's avatar

That means User::find($id) did not return a result. You can check that or use FindOrFail()

Shiva's avatar

I used the FindOrFail() and I got this error

No query results for model [User].

My model is User and it's being called. Here is my User model just in case.

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

 use UserTrait, RemindableTrait;

 protected $fillable = array('name', 'email', 'password');
 protected $guarded = array('id');

 /**
  * The database table used by the model.
  *
  * @var string
  */
 protected $table = 'users';

 /**
  * The attributes excluded from the model's JSON form.
  *
  * @var array
  */
 protected $hidden = array('password', 'remember_token');

 public static $rules = array(
  'name' => 'required',
  'email' => 'required',
  'password' => 'required'
  );

}

I don't understand why this has happend, because it worked fine before. The only difference between now and then is that I'm using creo's module package.

arabsight's avatar

It is your route parameter, try this in your Form::model:

'route' => array('admin.users.update', $users->id)
Shiva's avatar

I get this error

Route [admin.users.update, $users->id] not defined. 
Shiva's avatar

I have

Route::resource('admin/users', 'UsersController');

in my routes.php

and when I type php artisan routes I get

+--------+-----------------------------------+---------------------+---------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+-----------------------------------+---------------------+---------------------------+----------------+---------------+
| | GET|HEAD admin/content/pages | | Closure | | |
| | GET|HEAD admin/content/articles | | Closure | | |
| | GET|HEAD admin/content/products | | Closure | | |
| | GET|HEAD admin/content | | Closure | | |
| | GET|HEAD admin/shop | | Closure | | |
| | GET|HEAD admin | | UsersController@showLogin | | |
| | GET|HEAD admin/users | admin.users.index | UsersController@index | | |
| | GET|HEAD admin/users/create | admin.users.create | UsersController@create | | |
| | POST admin/users | admin.users.store | UsersController@store | | |
| | GET|HEAD admin/users/{users} | admin.users.show | UsersController@show | | |
| | GET|HEAD admin/users/{users}/edit | admin.users.edit | UsersController@edit | | |
| | PUT admin/users/{users} | admin.users.update | UsersController@update | | |
| | PATCH admin/users/{users} | | UsersController@update | | |
| | DELETE admin/users/{users} | admin.users.destroy | UsersController@destroy | | |
| | GET|HEAD admin/dashboard | | UsersController@dashboard | | |
| | POST admin | | UsersController@doLogin | | |
| | GET|HEAD / | | Closure | | |
+--------+-----------------------------------+---------------------+---------------------------+----------------+---------------+



bestmomo's avatar

Check your syntax with this :

Form::model($users, ['route' => ['admin.users.update', $users->id], 'method' => 'patch'])
arabsight's avatar

seems good, when i told you to update you form::model I meant this:

{{ Form::model($users, array('method' => 'PATCH', 'route' => array('admin.users.update', $users->id))) }}
bestmomo's avatar

There is something with your Laravel installation. Try a clear-compiled command and a dump-autoloed. If it doesn't work try to start on a fresh installation.

Shiva's avatar

ok will do that and get back to you

Shiva's avatar

I've installed a new laravel and I've done everything again. And I have this error

No query results for model [User].

which is the same as my post above

arabsight's avatar

I replicated your code and its working fine.

where exactly are getting the error.

Shiva's avatar

It happens when I update my user. It works fine when I create a new user.

arabsight's avatar

try to isolate the problem, check your edit method: is it returning $users variable correctly? if so is your form shown at all?

Shiva's avatar

My form does show when I click on the edit button and it gives the correct id in the url. But when I click submit to update I get this in the url

users/%7Busers%7D/edit
arabsight's avatar

that's a route param problem, you have to use: 'route' => array('admin.users.update', $users->id)

Shiva's avatar

This is what I have in my routes.php

Route::resource('admin/users', 'UsersController');

and this is what my routes looks like in the cmd

+--------+-----------------------------------+---------------------+---------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+-----------------------------------+---------------------+---------------------------+----------------+---------------+
| | GET|HEAD admin | | UsersController@showLogin | | |
| | GET|HEAD admin/users | admin.users.index | UsersController@index | | |
| | GET|HEAD admin/users/create | admin.users.create | UsersController@create | | |
| | POST admin/users | admin.users.store | UsersController@store | | |
| | GET|HEAD admin/users/{users} | admin.users.show | UsersController@show | | |
| | GET|HEAD admin/users/{users}/edit | admin.users.edit | UsersController@edit | | |
| | PUT admin/users/{users} | admin.users.update | UsersController@update | | |
| | PATCH admin/users/{users} | | UsersController@update | | |
| | DELETE admin/users/{users} | admin.users.destroy | UsersController@destroy | | |
| | GET|HEAD admin/dashboard | | UsersController@dashboard | | |
| | POST admin | | UsersController@doLogin | | |
| | GET|HEAD / | | Closure | | |
+--------+-----------------------------------+---------------------+---------------------------+----------------+---------------+
Shiva's avatar

I changed the route in the form to

'route' => array('admin.users.update', $users->id)

It just went to the index page with out saving anything.

My edit function in my UsersController is

public function edit($id)
 {
  $users = User::find($id);

  if(is_null($users)){
   return Redirect::route('admin.users.index');
  }

  return View::make('users::edit', compact('users'));
 }
arabsight's avatar

your routes are fine, the problem is in your edit view.

try to write it from scratch and test it step by step.

Shiva's avatar

will do. I will get back with whatever I find

Shiva's avatar
Shiva
OP
Best Answer
Level 5

I have finally fixed my problem. It was so simple I want to cry and bash my head against the table.

I changed

return View::make('users::index' , $id);

to

return Redirect::route('admin.users.index', $id);

and it worked

bashy's avatar

Can I just ask why you had users::index in this line anyway? :P

return View::make('users::index' , $id);
Shiva's avatar

It's how you call a view when your using creo's module package. And from how I understand it, it's like this

return View::make('module::view', $id)

Please or to participate in this conversation.