作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何根据所选字段获取结果。如果选择 car_type 来获取汽车,如果选择奥迪则仅获取奥迪车型。示例用户选择 car_type Car ,获得 Marks,用户选择 Mark Audi 获得 A1,A2。用户选择宝马X6、X5。我连接所有只需要从数据库接收数据。看我的代码。
汽车类型表:
$table->increments('id');
$table->string('name');
$table->timestamps();
分数表:
$table->increments('id');
$table->string('name');
$table->timestamps();
$table->integer('car_type_id');
型号表:
$table->increments('id');
$table->integer('mark_id');
$table->string('name');
$table->timestamps();
$table->integer('car_type_id');
Car_type 型号:
public function marks(){
return $this->hasMany(Mark::class);
}
标记型号:
public function car_type(){
return $this->belongsTo(Car_type::class);
}
public function carmodels(){
return $this->hasMany(CarModel::class);
}
CarModel 型号:
public function mark(){
return $this->belongsTo(Mark::class);
}
现在我需要在 Controller 中获取数据。我试试这个
$car_type = Car_type::where('id' , $id)->first();
return $car_type->marks()->get();
使用 car_type model 1 的 id 获取标记,但这是错误的逻辑。
最佳答案
您可以直接使用汽车标记进行一次查询,而不是两次查询。
return Mark::where('car_type_id', $id)->get();
关于php - 如何在 Laravel Controller 中获取 Car_type、Marks 和 model?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55178648/
如何根据所选字段获取结果。如果选择 car_type 来获取汽车,如果选择奥迪则仅获取奥迪车型。示例用户选择 car_type Car ,获得 Marks,用户选择 Mark Audi 获得 A1,A
我有汽车类型、品牌和型号表。当用户选择汽车类型时,他会选择奥迪、宝马、奔驰。选择奥迪时,仅获取奥迪A1、A2、A3等车型。如果宝马获胜获得X6、X4车型。我有三张 table 1. 使用汽车输入。 (
我是一名优秀的程序员,十分优秀!