Laravel 关系:hasOne 与 belongsToMany
Laravel relationships: hasOne vs belongsToMany
我有 3 个 table:
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
尝试了很多组合
知道那很愚蠢,但无能为力)
当我像这样填写 table 时:
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());
}
获取错误:
Column not found: 1054 Unknown column 'item_type_id' in 'field list'
(SQL: insert into item_types
(name
, item_type_id
) values (et,
1))
如果我在 Item.php 中设置它:
`hasOne('App\ItemType', 'id', 'item_type_id');`
品牌相同
所有 table 都已填满,但项目 table 中的 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();
你的人际关系错了。永远记住狗和主人的插图:
[…] Let's say we have a Dog model and an Owner model. Immediately we can say the Owner has_one
Dog and the Dog belongs_to
Owner.
在您的特定情况下,您的狗是物品,它们的主人是物品类型和品牌:
# Item.php
public function itemType()
{
return $this->belongsTo('App\ItemType');
}
public function brand()
{
return $this->belongsTo('App\Brand');
}
还有另外两个 类:
# ItemType.php
public function items()
{
return $this->hasMany('App\Item');
}
# Brand.php
public function items()
{
return $this->hasMany('App\Item');
}
编码愉快!
我有 3 个 table:
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
尝试了很多组合 知道那很愚蠢,但无能为力)
当我像这样填写 table 时:
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());
}
获取错误:
Column not found: 1054 Unknown column 'item_type_id' in 'field list' (SQL: insert into
item_types
(name
,item_type_id
) values (et, 1))
如果我在 Item.php 中设置它:
`hasOne('App\ItemType', 'id', 'item_type_id');`
品牌相同
所有 table 都已填满,但项目 table 中的 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();
你的人际关系错了。永远记住狗和主人的插图:
[…] Let's say we have a Dog model and an Owner model. Immediately we can say the Owner
has_one
Dog and the Dogbelongs_to
Owner.
在您的特定情况下,您的狗是物品,它们的主人是物品类型和品牌:
# Item.php
public function itemType()
{
return $this->belongsTo('App\ItemType');
}
public function brand()
{
return $this->belongsTo('App\Brand');
}
还有另外两个 类:
# ItemType.php
public function items()
{
return $this->hasMany('App\Item');
}
# Brand.php
public function items()
{
return $this->hasMany('App\Item');
}
编码愉快!