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

标签 php mysql laravel eloquent

首先看我的结构代码。我需要获得用户汽车的有限优惠。 我有硬编码 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');
}

Subscription_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);
}

最佳答案

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

$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/

相关文章:

php - 在 Javascript 中使用 PHP 检索变量

php - 通过它们扩展的测试用例类运行 phpunit 测试

mysql - 使用 Hive 查询 Sqoop 到 MySQL

php - 奇怪的 JSON 键?

laravel - laravel 的默认 cookie/session 中存储了什么?

mysql - Laravel:使用增量 ID 作为另一列的默认值

php - 命名空间如何在 PHP 中工作

javascript - 在 yii2 中选中复选框后启用字段

mysql - 从 rails 中的迁移脚本加载 csv 时遇到问题

php - Laravel 分页最新行(反向)