【发布时间】:2019-06-05 23:42:18
【问题描述】:
laravel 一对多关系模型使用Laravel-form-builder 示例。我有以下一对多关系模型
car_type model (one)
'id',
'name'
cars model (many)
'id',
'car_type_id',
'name'
我已经阅读了documentation,但我不明白。你能给我一个完整的例子吗?
【问题讨论】:
laravel 一对多关系模型使用Laravel-form-builder 示例。我有以下一对多关系模型
car_type model (one)
'id',
'name'
cars model (many)
'id',
'car_type_id',
'name'
我已经阅读了documentation,但我不明白。你能给我一个完整的例子吗?
【问题讨论】:
正确的文档在这里:https://laravel.com/docs/5.8/eloquent-relationships
每个 car_type(我称之为汽车 make)都有汽车。 (例如,有数百万辆丰田凯美瑞)此 cars() 方法将根据汽车 make (car_type) 检索所有相关汽车型号。一辆车只有一种类型(例如,一辆汽车不能同时是丰田凯美瑞和福特福克斯),因此汽车模型上的 type() 方法将检索汽车模型的品牌。
class car_type extends Model {
public function cars() {
return $this->hasMany(cars::class);
}
}
class cars extends Model {
public function type() {
return $this->belongsTo(car_type::class);
}
}
【讨论】: