flitedocnm
flitedocnm

Reputation: 91

"update" yielding "Creating default object from empty value" Laravel-4

Route:

Route::resource('users','UsersController')

UsersController:

public function edit($id)
{
    $current = Auth::user();
    $user = $this->user->find($id);
    return View::make('users.edit')->with('current',$current)->with('user',$user);
}

public function update($id)
{
    if( ! $this->user->isValid(Input::all()))
    {
    return Redirect::back()->withInput()->withErrors($this->user->errors);
    }

    $user = $this->user->find($id);

    $user->first_name = Input::get('first_name');
    $user->email = Input::get('email');
    $user->password = Hash::make(Input::get('password'));
...         
    $user->save();

    return Redirect::route('users.index');
}

User Model:

protected $table = 'users'; 

protected $fillable = ['username','email','password'];

public static $rules = [
    'first_name' => 'required',
    'last_name' => 'required',
    'email' => 'required',
    'password' => 'required'
];

edit.blade.php :

{{ Form::model($user, array('method' => 'put', 'route'=>array('users.update','$user'=>'id'))) }}
...
{{ Form::submit('Update User', array('class'=>'button')) }}

The validation piece seems to work fine; if I leave out a required field, it redirects back with error messages and input. If all required fields are filled, then it yields the error: "Creating default object from empty value" and quotes the starting code from the update function on the UsersController. The same code works fine with the create function. I don't see where I'm making an error. Thanks!

Upvotes: 0

Views: 1360

Answers (1)

Laurence
Laurence

Reputation: 60048

You are not passing the user ID correctly in the form method.

Change

{{ Form::model($user, array('method' => 'put', 'route'=>array('users.update','$user'=>'id'))) }}

to

{{ Form::model($user, array('method' => 'put', 'route'=>array('users.update',$user->id))) }}

Upvotes: 2

Related Questions