本文介绍了自动将值分配给$ fillable属性(Laravel 4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Laravel 4将数据从Localhost数据库同步到Live数据库.除了表中列是动态的之外,其他所有方法都可以正常工作.所以在我的模型中,我做了类似的事情:
I'm trying to Sync data from a Localhost database to a Live one using Laravel 4. Everything works fine except for a table where columns' are dynamic. So in my Model I did something like:
<?php
class myModel extends \Eloquent {
protected $fillable = [];
protected $connection = 'live';
protected $table = "myLiveTable";
public function __construct()
{
$this->setFillable();
}
public function setFillable()
{
$fields = someSQLHandler::getColumns('myLocalTable');
$this->fillable = $fields;
}
}
为了防止在$fillable
中手动键入字段,因为这是不可能的!
in order to prevent typing the fields in $fillable
manually as it is impossible!
即使我知道这是一种不好的做法,有什么方法可以使其自动分配或至少防止批量分配"错误?
Is there any way to make it automatically assigned or at least prevent the Mass Assignment error even if I know it's a bad practice?
谢谢!
推荐答案
如何使用黑名单而不是白名单:
How about using a black-list instead of a white-list:
protected $guarded = array('id', 'random_column');
然后完全删除$fillable
.
这篇关于自动将值分配给$ fillable属性(Laravel 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!