在 laravel 4 中存储与枢轴 table 的关系
Store relationship with pivot table in laravel 4
让我解释一下我试图实现的目标。
假设我有 20 种车型,例如跑车或家用车等,还有 5 种车型,例如保时捷。
当我创建汽车时,我可以选择同时检查属于该汽车的多种车型并保存它。
我做了一些功课,看起来使用枢轴 table 是在 laravel.
中实现此目的的方法
我的汽车模型中有这个方法:
public function types()
{
return $this->belongsToMany('types', 'car_types');
}
我的类型模型中的这个方法:
public function cars()
{
return $this->belongsToMany('Car');
}
我的 table 看起来像这样:
汽车
- 编号
- 名字
- created_at
- updated_at
类型
- 编号
- 名字
- created_at
- updated_at
car_types
- car_id
- type_id
我在我的控制器中试图做的是:
$car = new Car();
Car::create( Input::except('types') );
foreach(Input::get('types') as $type)
{
$car->types()->associate($type);
$car->save();
}
这给我以下错误:
调用未定义的方法 Illuminate\Database\Query\Builder::associate()
我希望有人能帮我解决这个问题。
提前致谢。
好吧,你快到了。你是对的,Car
和 Type
这两个模型是 many to many
关系。
在您的模型中,您有这样的代码:
return $this->belongsToMany('types', 'car_types');
和
return $this->belongsToMany('Car');
错误 #1:
在 Car
模型的 types()
方法中,作为第一个参数,您应该传递模型的名称,而不是 table 的名称。所以你应该将其更改为:
return $this->belongsToMany('Type', 'car_types');
错误 #2
在您的 Car
模型中,您将枢轴 table 定义为 car_types
,但 Type
模型中缺少枢轴定义。从 Car
模型中的 types()
方法中删除 , 'car_types'
并将你的枢轴 table 重命名为 car_type
,或者将 , 'car_types'
添加到你的 [= Type
模型中的 27=] 方法。
错误 #3
正确设置所有模型后,您可以在控制器中执行此操作:
$car = new Car();
$car->name = Input::get('car_name_form_field_name_attribute');
$car->save();
$types = Input::get('types');
$car->types()->sync($types);
错误 #4
我不确定这是否只是一个 copy/paste 错误,但您的数据透视表 table 似乎缺少主键字段。每个 table 都需要一个主键字段,因此您的 car_types
table 应该如下所示:
car_types
- id
- car_id
- type_id
您也可以使用 attach()
而不是 sync()
。更多关于这两者之间的区别 here
请试用此代码,如果成功请告诉我们。
让我解释一下我试图实现的目标。 假设我有 20 种车型,例如跑车或家用车等,还有 5 种车型,例如保时捷。
当我创建汽车时,我可以选择同时检查属于该汽车的多种车型并保存它。
我做了一些功课,看起来使用枢轴 table 是在 laravel.
中实现此目的的方法我的汽车模型中有这个方法:
public function types()
{
return $this->belongsToMany('types', 'car_types');
}
我的类型模型中的这个方法:
public function cars()
{
return $this->belongsToMany('Car');
}
我的 table 看起来像这样:
汽车
- 编号
- 名字
- created_at
- updated_at
类型
- 编号
- 名字
- created_at
- updated_at
car_types
- car_id
- type_id
我在我的控制器中试图做的是:
$car = new Car();
Car::create( Input::except('types') );
foreach(Input::get('types') as $type)
{
$car->types()->associate($type);
$car->save();
}
这给我以下错误:
调用未定义的方法 Illuminate\Database\Query\Builder::associate()
我希望有人能帮我解决这个问题。
提前致谢。
好吧,你快到了。你是对的,Car
和 Type
这两个模型是 many to many
关系。
在您的模型中,您有这样的代码:
return $this->belongsToMany('types', 'car_types');
和
return $this->belongsToMany('Car');
错误 #1:
在 Car
模型的 types()
方法中,作为第一个参数,您应该传递模型的名称,而不是 table 的名称。所以你应该将其更改为:
return $this->belongsToMany('Type', 'car_types');
错误 #2
在您的 Car
模型中,您将枢轴 table 定义为 car_types
,但 Type
模型中缺少枢轴定义。从 Car
模型中的 types()
方法中删除 , 'car_types'
并将你的枢轴 table 重命名为 car_type
,或者将 , 'car_types'
添加到你的 [= Type
模型中的 27=] 方法。
错误 #3
正确设置所有模型后,您可以在控制器中执行此操作:
$car = new Car();
$car->name = Input::get('car_name_form_field_name_attribute');
$car->save();
$types = Input::get('types');
$car->types()->sync($types);
错误 #4
我不确定这是否只是一个 copy/paste 错误,但您的数据透视表 table 似乎缺少主键字段。每个 table 都需要一个主键字段,因此您的 car_types
table 应该如下所示:
car_types
- id
- car_id
- type_id
您也可以使用 attach()
而不是 sync()
。更多关于这两者之间的区别 here
请试用此代码,如果成功请告诉我们。