gpt4 book ai didi

php - Mysql 不接受表单输入 Laravel 5.2

转载 作者:行者123 更新时间:2023-11-29 11:29:37 25 4
gpt4 key购买 nike

PHP 和 Laravel 5.2 新手。我使用 php artisan make:auth 创建了一个用户表,但想在我的数据库中添加两列。我能够在数据库中添加列,但是当我注册新用户时,它接受除 mysql 中的名字和姓氏以外的所有内容,这些列显示为空白。我不明白我错过了什么?

    Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('avatar')->default('default.jpg');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});

我的 AuthController 文件

   protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}


protected function create(array $data)
{
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);

register.blade 上名字的表单输入示例

<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">First Name</label>

<div class="col-md-6">
<input type="text" class="form-control" name="first_name" value="{{ old('first_name') }}">

@if ($errors->has('first_name'))
<span class="help-block">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
@endif
</div>
</div>

最佳答案

正如 @Sid 所说,这些字段需要可批量分配,这在文档中进行了描述:

You may also use the create method to save a new model in a single line. [...] before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment.

来自https://laravel.com/docs/5.2/eloquent#mass-assignment

class Flight extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name'];
}

关于php - Mysql 不接受表单输入 Laravel 5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37640038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com