【发布时间】:2019-03-15 08:45:31
【问题描述】:
如何根据所选字段获取结果。如果选择 car_type 获取汽车,如果选择 audi 仅获取 audi 模型。示例用户选择 car_type Car ,获得标记,用户选择 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');
车型型号:
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);
}
车型:
public function mark(){
return $this->belongsTo(Mark::class);
}
现在我需要在控制器中获取数据。我试试这个
$car_type = Car_type::where('id' , $id)->first();
return $car_type->marks()->get();
获取 ID 为 car_type 模型 1 的标记,但这是错误的逻辑。
【问题讨论】:
-
但这是错误的逻辑。 为什么?您可以编写一个一次性完成所有操作的查询,但问题是什么?
标签: php mysql laravel eloquent