【问题标题】:Automatically Assign Values To $fillable attribute (Laravel 4)自动将值分配给 $fillable 属性(Laravel 4)
【发布时间】:2015-02-22 20:44:23
【问题描述】:

我正在尝试使用 Laravel 4 将数据从 Localhost 数据库同步到 Live 数据库。除了列是动态的表之外,一切正常。所以在我的模型中我做了类似的事情:

<?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中的字段,因为这是不可能的!

有没有办法让它自动分配或至少防止批量分配错误,即使我知道这是一种不好的做法?

谢谢!

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

    如何使用黑名单而不是白名单:

    protected $guarded = array('id', 'random_column');
    

    并彻底删除$fillable。

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多