gpt4 book ai didi

php - 以有限的优惠获取汽车 Laravel

转载 作者:行者123 更新时间:2023-11-29 01:48:51 25 4
gpt4 key购买 nike

先看我的结构代码。我需要获得用户汽车的有限优惠。我有硬编码 3 包。注册后免费使用。银和金。如果用户订阅白银,则看到 8 个优惠如果订阅黄金,则看到 15 个优惠。如果没有订阅,请查看 2 个优惠(免费套餐)。我的代码:

用户迁移:

        $table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();

用户模型:

public function subscriptions_users(){
return $this->hasMany('App\Subscription');
}
public function cars(){
return $this->hasMany('App\Car');
}
public function offers(){
return $this->hasMany('App\Offer');
}

汽车迁移:

 $table->bigIncrements('id');
$table->string('car_type');
$table->string('mark');
$table->string('model');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

车型:

public function images() {
return $this->hasMany(CarImages::class);
}
public function user() {
return $this->belongsTo('App\User');
}
public function offer() {
return $this->hasMany('App\Offer');
}

提供迁移:

        $table->increments('id');
$table->integer('price');
$table->unsignedInteger('user_id');
$table->unsignedInteger('car_id');

提供模型:

public function car() {
return $this->belongsTo('App\Car');
}
public function user() {
return $this->belongsTo('App\User');
}

订阅迁移:

        $table->increments('id');
$table->string('subscription');
$table->integer('numberOfOffers');

订阅模式:

public function users(){
return $this->hasMany('App\User');
}

订阅用户(包含用户和订阅的数据透视表):

        $table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); //foreign key relation
$table->integer('subscription_id')->unsigned()->index();
$table->foreign('subscription_id')->references('id')->on('subscriptions')->onDelete('cascade');

订阅用户模型:

public function users(){
return $this->belongsToMany('App\User');
}

public function subscriptions() {
return $this->belongsToMany('App\Subscription');
}

Everythink 工作正常,只是我需要获得限量版汽车的优惠。有限版本是字段 $table->integer('numberOfOffers');在订阅表中。我的关系可能有问题。我尝试了这个但没有成功:

public function getOffers($carID) {
$car = Car::find($carID);
$offers = $car->offer()->limit(THIS IS PROBLEM I DON'T KNOW HOT TO CONNECT USER WITH numberOfOffers)->get();
return response()->json($offers);
}

最佳答案

当你需要查询关系时,你应该使用这样的with方法:

$offers = Car::where('id', $carId)->with(['offer' => function($query) use ($limit){
return $query->take($limit);
}])->get();

关于php - 以有限的优惠获取汽车 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55004926/

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