Automatically Assign Values To $fillable attribute (Laravel 4)

流过昼夜 提交于 2019-12-13 01:24:02

问题


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;
    }
} 

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?

Thanks!


回答1:


How about using a black-list instead of a white-list:

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

And remove the $fillable completely.



来源:https://stackoverflow.com/questions/27641854/automatically-assign-values-to-fillable-attribute-laravel-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!