在Laravel 5中保存/更新用户配置文件 [英] Saving/Updating User Profile in Laravel 5
问题描述
我似乎无法将更新的配置文件保存到数据库中.
I can't seem to save the updated profile to the database.
在我的edit.blade.php中:
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() !!}
在我的ProfilesController中:
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('/');
}
点击更新按钮后,它会在首页上显示Flash消息,但不会保存到数据库中.我来自Rails,我觉得我需要将某些事情列入白名单.
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.
推荐答案
要点是,您根本不需要更改用户模型...您可以检索它,然后在不设置任何字段的情况下再次保存它.
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
如果您将上述方法与
$user->fill(\Input::all());
您必须像这样向用户模型添加$ fillable数组
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();
如果您尝试了所提供的annmok,您可能会因为使用过而获得"whooops"页面
If you have tried the anwer Sinmok provided, you probably get the "whooops" page because you used
Input::get('field');
代替
\Input::get('field');
在您的Blade语法上,我假设您使用laravel 5. 因此,由于您的控制器具有命名空间,因此必须在Input之前添加\来引用根命名空间(或在类顶部放置use语句)
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)
通常,应在开发服务器上启用调试.然后,除了纯粹的问题之外,您还将获得更多有关发生问题的详细信息."whoops ..."
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... "
您可以在config/app.php文件中进行设置
In your config/app.php file you can set
'debug' => true;
或
您可以查看 http://laravel.com/docs/5.0/configuration 并使用.env文件.
you have a look at http://laravel.com/docs/5.0/configuration And use the .env file.
如果您使用.env文件,请确保其中包含类似
If you use the .env file make sure there is an entry with something like
APP_DEBUG=true
然后您可以使用
'debug' => env('APP_DEBUG'),
在您的安装中应该有一个.env.example实例,以使您了解此类文件的外观.
There should be a .env.example in your installation to give you a clue how such a file could look like.
这篇关于在Laravel 5中保存/更新用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!