Laravel 为什么除一个字段外所有字段都正确存储?
我正在创建一个这样的提供商:Laravel 为什么除一个字段外所有字段都正确存储?,laravel,Laravel,我正在创建一个这样的提供商: $provider = Provider::create([ 'first_name' => $data['first_name'], 'last_name' => $data['last_name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), 'is_available' => 1, 'is_
$provider = Provider::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'is_available' => 1,
'is_activated' => 1,
'insurance' => 1
]);
Log::info($provider);
class AddInsuranceToProvidersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('providers', function($table) {
$table->integer('insurance');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
提供程序实际上创建得很好,除保险字段外,所有字段都正确存储
保险领域不起作用,我不知道为什么
它是使用Laravel迁移工具创建的,如下所示:
$provider = Provider::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'is_available' => 1,
'is_activated' => 1,
'insurance' => 1
]);
Log::info($provider);
class AddInsuranceToProvidersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('providers', function($table) {
$table->integer('insurance');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
然后从终端:
php artisan migrate
php artisan cache:clear
下面是phpMyAdmin的图片:
你知道为什么我不能为保险业保存任何价值吗
另外,当我记录$provider变量时,与保险字段没有任何关系,如您所见:
[2017-12-13 12:36:54]local.INFO:
{“名字”:“fef”,“姓氏”:“ewfe”,“电子邮件”:“fel”。bruno2njtz3@gmail.com,“是否可用”:1,“是否激活”:1,“更新时间”:“2017-12-13
12:36:54,“创建时间”:“2017-12-13 12:36:54”,“id”:276}
多亏了这些答案,我才能够通过将保险字段添加到提供者模型中来解决这个问题,因为它现在看起来是这样的:
protected $fillable = [
'name','first_name','last_name', 'email', 'password','is_available','is_activated','insurance'
];
如果您在模型中设置了一个“可填充”数组,则必须将“保险”字段添加到该数组中。由于您使用迁移更改了表,您只需在应用程序目录中更新
InsuranceProvider
模型,并将insurance
字段添加到可填充数组中,即可使其正常工作。因为您使用的是create()
方法,您需要将该字段添加到$filleble
数组中:
protected $fillable = ['insurance', ....];
您需要这样做,因为create()
正在使用fill()
方法为您填充模型字段
你是否在你的模型中添加了
保险
字段?不,我在这个项目中从来没有碰过任何模型,而且通常都有效。不,他没有。仅当他使用可填充数组时。这是可选的。@Amarnasan当您使用create()
方法时,它不是可选的。如果不填充filleble
或guarded
数组,它将无法工作。在上面的一条评论中,您告诉过filleble
是可选的,但它不是。因此,不是“如果设置…”,而是始终需要使用filleble
或guarded
使create()
工作,因为它在内部使用fill()
方法。因此,如果您不定义它,create()
将跳过所有不在filleble
@alexeymezen中的字段。是的,您已经对此进行了注释。为什么在这里重复这个评论?你打算在网站上追查我所有的答案并发表同样的评论吗-D