laravel创建省略/跳过字段

时间:2014-04-06 16:30:53

标签: php laravel

我正在尝试使用laravel播种机播种几个数据库,但似乎它会跳过某些字段..

我这样做:

Item::create(array(
    'name' => 'Category 2',
    'photo' => 'Description of category 2',
    'order' => 1
    'price' => 2.30
    'category_id' => 1,
));

数据库中没有设置字段'category_id'和顺序..

我用这种方式打电话给播种机

Artisan::call('db:seed',array('--database' => 'tenant_'.$user, '--class' => 'TenantSeeder'));

知道为什么会这样吗?

如果我使用标准

$item = new Item;
$item->allfields = "its_value";
$item->save();

完美无缺

UPDATE 这是模型:

<?php
class Item extends Model {

    //============================================================================
    // PARENT VARIABLES
    //============================================================================
    protected $table = "items";

    protected $softDelete = true;

    protected $hidden = ['created_at','updated_at'];
    protected $fillable = ['name','price','description','photo'];   //Items that can be mass assigned
    protected $guarded = array('id');

    protected static $rules = [
        'name'          => 'required|min:3',
        'price'         => '',
        'description'   => '',
    ];

    //============================================================================
    // METHODS
    //============================================================================
    public function getId()         {   return $this->getKey();  }
    public function getName()       {   return $this->name;      }
    public function getPhotoSrc()   {   return $this->photo;     }

    public function item_category(){
        return $this->belongsTo('Items_category');
    }

    public function scopeActive($query)
    {
        return $query->where('active', '=', true);
    }
}

和方案

Schema::create('items', function(Blueprint $table)
        {
            // auto increment id (primary key)
            $table->increments('id');

            $table->string('name')->default('New Item');
            $table->float('price')->default(0);
            $table->string('photo')->nullable();
            $table->integer('order')->unsigned();
            $table->boolean('active')->default(true);
            $table->string('description')->nullable();

            $table->integer('category_id')->unsigned();
            $table->foreign('category_id')->references('id')->on('items_categories')->onDelete('cascade');

            // created_at, updated_at DATETIME
            $table->timestamps();
            $table->softDeletes(); //It is not really deleted, just marked as deleted
        });

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

正如User2094178所说, 我没有$ fillable中的字段