php 在 Laravel 5 中保存/更新用户配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29498775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:26:38  来源:igfitidea点击:

Saving/Updating User Profile in Laravel 5

phplaravellaravel-5

提问by Sylar

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

我似乎无法将更新的配置文件保存到数据库中。

In my edit.blade.php:

在我的 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:

在我的 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.

点击更新按钮后,它会在主页上显示 Flash 消息,但它没有保存到数据库中。我来自 Rails,我觉得我需要将某些东西列入白名单。

回答by shock_gone_wild

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

你必须在你的用户模型中添加一个 $fillable 数组,比如

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

如果您尝试过 Sinmok 提供的 anwer,您可能会看到“whooops”页面,因为您使用了

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)

在您的 Blade Syntax 中,我假设您使用 laravel 5。因此,由于您的控制器是命名空间的,因此您必须在 Input 之前添加一个 \ 以引用根命名空间(或在类的顶部放置一个 use 语句)

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

在您的 config/app.php 文件中,您可以设置

'debug' => true;

OR

或者

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

你看看http://laravel.com/docs/5.0/configuration并使用 .env 文件。

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

如果您使用 .env 文件,请确保有一个类似

APP_DEBUG=true

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

然后你可以在你的 config/app.php 中访问该值

'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.

您的安装中应该有一个 .env.example 来为您提供此类文件的外观的线索。

回答by nageen nayak

UserController update function look like that :-

UserController 更新功能看起来像这样:-

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!!');
    }

回答by Sinmok

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

看起来您尚未将提交值设置为用户对象。

Try (Update this is for Laravel 4)

尝试(更新这是针对 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:

实际上,如果您使用的是 Laravel 5,它看起来应该是:

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