Php Laravel:嵌套关系在哪里?

Php Laravel:嵌套关系在哪里?,php,laravel,Php,Laravel,在我的应用程序中,我有场地,每个场地都有折扣,每个折扣都有代码可以兑换 为了兑换折扣代码,我必须寻找属于折扣和特定场地的折扣代码,我知道这必须用where has来完成,但我不知道如何实现这一点 这就是我到目前为止所做的: $code = $request['code']; $venue_id = $request['venue_id']; $discount_id = $request['discount_id']; $codeExists= DiscountCode::where('uni

在我的应用程序中,我有场地,每个场地都有折扣,每个折扣都有代码可以兑换

为了兑换折扣代码,我必须寻找属于折扣和特定场地的折扣代码,我知道这必须用where has来完成,但我不知道如何实现这一点

这就是我到目前为止所做的:

$code = $request['code'];
$venue_id = $request['venue_id'];
$discount_id = $request['discount_id'];

$codeExists= DiscountCode::where('unicode', $code])
            ->where('purchasedDate', '!=', null)
            ->with(['discount.venue', 'user'])
            ->first();
有人能帮我吗

我的模型的模式:

Schema::create('discountcodes', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('discount_id')->index();
            $table->foreign('discount_id')->references('id')->on('discounts')->onDelete('cascade')->onUpdate('cascade');
            $table->unsignedInteger('order_id')->index()->nullable();
            $table->foreign('order_id')->references('id')->on('orders')->nullable()->onDelete('cascade')->onUpdate('cascade');
            $table->unsignedInteger('user_id')->index()->nullable();
            $table->foreign('user_id')->references('id')->on('users')->nullable()->onDelete('cascade')->onUpdate('cascade');
            $table->string('unicode')->unique();
            $table->timestamp('purchasedDate')->nullable();
            $table->timestamp('usedDate')->nullable();
            $table->timestamp('expirationDate')->nullable();
            $table->timestamps();
        });

Schema::create('discounts', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('discountcategory_id')->index()->default(1);;
            $table->foreign('discountcategory_id')->references('id')->on('discountcategories')->onDelete('cascade')->onUpdate('cascade');
            $table->unsignedInteger('venue_id')->index()->default(1);
            $table->foreign('venue_id')->references('id')->on('venues')->onDelete('cascade')->onUpdate('cascade')->default(1);
            $table->string('title')->unique();
            $table->timestamp('expireDate');
            $table->timestamps();
        });

Schema::create('venues', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('address_id')->index();
            $table->foreign('address_id')->references('id')->on('address')->onDelete('cascade')->onUpdate('cascade');
            $table->string('name');
            $table->string('image')->nullable();
            $table->timestamps();
        });

类似的内容可以帮助您吗?

您可以为这三个表添加模式吗?
DiscountCode::where('unicode', $code)
   ->whereHas('discount', function($q) { 
       $q->where('discounts.id', $discount_id);
 })->whereHas('discount.venue', function($q2) { 
       $q2->where('venues.id', $venue_id); })->exists()