gpt4 book ai didi

php - 如何在 Laravel Controller 中获取 Car_type、Marks 和 model?

转载 作者:行者123 更新时间:2023-11-29 16:11:59 26 4
gpt4 key购买 nike

如何根据所选字段获取结果。如果选择 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com