【发布时间】:2015-12-12 05:16:47
【问题描述】:
我有 3 张桌子:
Schema::create('item_types', function (Blueprint $table) {
$table->increments('id')->unsignet();
$table->string('name')->unique();
});
Schema::create('items', function (Blueprint $table) {
$table->increments('id')->unsignet();
$table->string('name');
$table->text('description');
$table->string('photo');
$table->integer('item_type_id')->unsignet(); //->nullable();
$table->integer('brand_id')->unsignet(); //->nullable();
$table->float('price')->unsignet();
$table->timestamps();
});
Schema::create('brands', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('name');
$table->string('logo');
$table->text('description');
});
需要它们之间的关系...
所以我设置:
hasOne for item_types and brands in Item.php
belongsToMany for Items in ItemType.php and Brand.php
尝试了很多组合 知道这很愚蠢,但什么也做不了)
当我像这样填写表格时:
factory(App\Item::class, 5)->create()->each(function($i) {
$i->type()->save(factory(App\ItemType::class)->make());
$i->brand()->save(factory(App\Brand::class)->make());
}
出现错误:
未找到列:1054 “字段列表”中的未知列“item_type_id” (SQL:插入
item_types(name,item_type_id)值(等, 1))
如果我在 Item.php 中设置:
`hasOne('App\ItemType', 'id', 'item_type_id');`
品牌也一样
所有表都填满了,但 items 表中的 item_type_id 和 brand_id 为空
[已解决]
下面回答+
factory(App\Item::class, 50)->create()->each(function($i) {
$i->ItemType()
->associate(factory(App\ItemType::class)
->create());
$i->brand()
->associate(factory(App\Brand::class)
->create());
$i->save();
【问题讨论】:
-
你的人际关系错了。 ;)
标签: laravel activerecord orm eloquent one-to-many