我有三个必须连接的表。用户、汽车、报价。每辆车都可以有很多优惠。用户可以拥有很多汽车(即连接)。我现在的任务是将 Car 与他们的报价和用户联系起来,但我现在有点困惑。看看我的迁移和模型。问题很简单,如何将 Car 与 Offers 和 Users 联系起来。一位用户可以发送一份报价。
汽车迁移:
$table->bigIncrements('id');
$table->string('car_type');
$table->string('mark');
$table->longText('car_accessories');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
提供迁移:
$table->increments('id');
$table->integer('price');
$table->unsignedSmallInteger('user_id');
$table->unsignedSmallInteger('car_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('car_id')->references('id')->on('cars')->onDelete('cascade');
$table->timestamps();
汽车模型:
public function user() {
return $this->belongsTo('App\User');
}
用户型号:
public function cars(){
return $this->hasMany('App\Car', 'user_id');
}
提供型号:
?