Saving/Updating User Profile in Laravel 5

前端 未结 3 1043
-上瘾入骨i
-上瘾入骨i 2021-01-05 14:38

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

In my edit.blade.php:

{!! Form::model($user, [\'method\' => \'PATCH\', \'route\' =>          


        
3条回答
  •  甜味超标
    2021-01-05 15:24

    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.

提交回复
热议问题