7

I can't seem to save the updated profile to the database.

In my edit.blade.php:

{!! Form::model($user, ['method' => 'PATCH', 'route' => ['profile.update', $user->company_name] ]) !!}

   // fields

  {!! Form::submit('Update Profile', ['class' => 'btn btn-primary']) !!}

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

In my ProfilesController:

public function update($company_name)

{
  $user = User::whereCompanyName($company_name)->firstOrFail();
  $user->save(); // no validation implemented
  flash('You have successfully edited your profile');
  return redirect('/');

}

After hitting the update button, it shows the flash message on the homepage but the it's not saving to the database. Im coming from Rails and I feel I need to whitelist something.

3 Answers 3

9

The point is, you don't change your user model at all... You retrieve it, and then save it again without setting any fields.

$user = User::whereCompanyName($company_name)->firstOrFail();
// this 'fills' the user model with all fields of the Input that are fillable
$user->fill(\Input::all());
$user->save(); // no validation implemented

If you are using the above method with

$user->fill(\Input::all());

you have to add a $fillable array to your User Model like

protected $fillable = ['name', 'email', 'password']; // add the fields you need

If you explicitly want to set only one or two ( or three....) field you could just update them with

$user->email = \Input::get('email');  // email is just an example.... 
$user->name = \Input::get('name'); // just an example... 
... 
$user->save();

If you have tried the anwer Sinmok provided, you probably get the "whooops" page because you used

Input::get('field');

instead of

\Input::get('field');

On your Blade Syntax i assume you use laravel 5. So as your controller is namespaced you have to add a \ before Input to reference the root namespace ( or put a use statement on top of your class)

Generally on your development server you should enable debugging. Then you have more detailed information about what's going wrong than just the pure.. "whoops... "

In your config/app.php file you can set

'debug' => true;

OR

you have a look at http://laravel.com/docs/5.0/configuration And use the .env file.

If you use the .env file make sure there is an entry with something like

APP_DEBUG=true

then you can access that value in your config/app.php with

'debug' => env('APP_DEBUG'),

There should be a .env.example in your installation to give you a clue how such a file could look like.

Sign up to request clarification or add additional context in comments.

3 Comments

This worked: $user->fill(Input::all());, no "\Input". Thanks!
In my TestController (Laravel 5) I needed to use \Input , but it's fine when it works for you ;)
This always save instead update. There is an id in the \Input::all()
2

UserController update function look like that :-

public function update(Request $request)
    {
        $user = Auth::user();

        $data = $this->validate($request, [
            'name' => 'required',
            'email' => 'required',
        ]);

        $user->name = $data['name'];
        $user->email = $data['email'];

        $user->save();
        return redirect('/user_edit/'.Auth::user()->id)->with('success', 'User has been updated!!');
    }

Comments

1

Looks like you've not set the submission values to the user object.

Try (Update this is for Laravel 4)

 $user = User::whereCompanyName($company_name)->firstOrFail();
 $user->field = Input::get("some_field"); // Name of the input field here
 $user->save(); // no validation implemented
 flash('You have successfully edited your profile');
 return redirect('/');

EDIT

Actually, if you're using Laravel 5 it looks like it should be:

$user->field = Request::input('some_field'); // Name of the input field here
$user->save(); // no validation implementedenter code here

1 Comment

Tried that but I got Whoops, looks like something went wrong. and the page does not redirect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.