在我的应用中,我有场馆,每个场馆都可以享受折扣,每个折扣都可以兑换代码。
为了兑换折扣代码,我必须寻找属于折扣和特定地点的折扣代码,我知道这必须在whereHas处完成,但是我不能全神贯注于如何做到这一点?
这是我到目前为止所拥有的:
$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();
});
答案 0 :(得分:0)
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()
类似的东西可以帮助您吗?