Table 列稍后填写

Table column filled later on

我是 Laravel 的新手,在使用它时遇到了这个问题。 我有一个运行良好的注册系统,但现在我想在我的 table(用户描述字段)中添加一个新字段。 但是,这个描述字段,我不想在用户注册时填写,我希望用户在他进入他的个人资料并更新模式时填写这个 window.

问题是,如果我让那个字段为空,我在注册时会收到一个错误,提示字段描述不能为空。

这是我在 UserController 中用来更新描述字段的内容,但我不确定是否正确。

public function postDesc(Request $request){
  $this->validate($request, [
    'description' => 'required|min:20'
  ]);
  $user = User::all();
  $user->description = $request->input('description');
  $user->save();
  return redirect()->route('user.profile.edit');
}

我是这样打开表格的: {!! Form::open(['method' => 'PUT', 'action' => 'UserController@postDesc', 'class' => 'profile-form']) !!}

您使用 required 验证规则,这就是您收到消息的原因。您应该对注册页面和个人资料更新表单使用不同的验证规则。

好的做法是 create two Request classes 并使用它们来验证两个表单。

在这种情况下,我宁愿保留您的描述栏 nullalbe()。所以在注册的时候不会报描述字段为空的错误。

稍后您可以更新描述字段。

public function postDesc(Request $request)
{
    $this->validate($request, [
    'description' => 'required|min:20'
    ]);

    // Get the logged in user id using auth and then updating description filed
    $user = User::where('user_id',  Auth::id())
               ->update([
               'description' => $request->description
               ]);

    return redirect()->route('user.profile.edit');
}