Eloquent车辆与价格的关系

Eloquent relationship between vehicle and price

对 eloquent 关系有点困惑。车辆与其价格之间的关系是什么,我需要一个支点 table!

下面是我的车辆车辆迁移。目标是能够查询与特定价格关联的所有车辆。

    class CreateVehiclesTable extends Migration
    
        {
        
            public function up()
            {
                Schema::create('vehicles', function (Blueprint $table) {
                    $table->id();
        
                    $table->string('title', 256);
                    $table->string('image')->nullable();
                    $table->integer('engineSize')->nullable();
                    $table->integer('numberSeats')->nullable();
                    $table->integer('mileage')->nullable();
                    $table->integer('power')->nullable();
                    $table->integer('doors')->nullable();
                    $table->string('fuel')->nullable();
                    $table->string('gearbox')->nullable();
                    $table->string('color')->nullable();
        
        
                    $table->integer('condition_id');
                    $table->integer('type_id');
                    $table->integer('modell_id');
                    $table->integer('year_id');
                    $table->integer('price');
        
        
                    $table->timestamps();
                });
            }
        
      
            public function down()
            {
                Schema::dropIfExists('vehicles');
            }
        }

假设表示车辆 table(您的迁移)记录的模型是 Vehicle

您可以通过

获取与特定价格相关的所有记录 - 例如 $price = 100
$vehiclesAssociatedWithPrice100 = Vehicle::where('price', 100)->get();

您应该阅读 Laravel 文档,它们真的是一目了然