dongtie0929 2014-04-06 16:30
浏览 26
已采纳

laravel创建省略/跳过字段

I'm trying to seed a couple of databases with laravel seeder, but it seems that it skips some fields..

I'm doing it this way:

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

The fields 'category_id' and order are not setup in the database..

I'm calling the seeder this way

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

Any idea why does it happen?

If I use the standard

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

It works perfectly

UPDATE Here comes the model:

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

And the Scheme

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

Any idea?

  • 写回答

1条回答 默认 最新

  • douji0108 2014-04-08 07:45
    关注

    As User2094178 said, I didn't have the fields in the $fillable

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型