雄辩的多态性hasManyThrough引发Cardinality违规:1222

时间:2019-11-12 08:50:41

标签: mysql laravel eloquent laravel-6.2

表结构:

餐厅:

id
title
description
etc....

折扣:

id
restaurant_id
date
percent

特殊折扣:

id
restaurant_id
from
to
percent

预订:

id
user_id
discount_id
discount_type
reserved_at

型号:

class Reservation extends Model{
    public function discount()
    {
        return $this->morphTo();
    }
}

我有两种类型的折扣:

class Discount extends Model
{
    public function reservations()
    {
        return $this->morphMany(Reservation::class, 'discount');
    }
}

和SpecialDiscount:

class SpecialDiscount extends Model
{
   public function reservations()
    {
        return $this->morphMany(Reservation::class, 'discount');
    }
}

SpecialDiscountDiscount属于Restaurant

现在我正在尝试通过折扣访问Restaurant Reservations

Restaurant模型中:

  public function reservations()
  {
      $relation = $this
          ->hasManyThrough(Reservation::class, Discount::class, 'restaurant_id', 'discount_id')
          ->where('discount_type', Discount::class)->getBaseQuery();

      $relation->union($this->hasManyThrough(Reservation::class, SpecialDiscount::class, 'restaurant_id', 'discount_id')
          ->where('discount_type', SpecialDiscount::class)
          ->getBaseQuery()
          ->select('reservations.*')
      );

      return $relation;
  }

所以它出错了:

  

SQLSTATE [21000]:基数违反:1222使用的SELECT   语句具有不同的列数(SQL :(从*中选择*   reservations上的discounts内部联接discountsid =   reservationsdiscount_id,其中discountsdeleted_at为空   和discount_type = App \ Models \ Discount)联合(选择   reservations。*来自reservations内部联接special_discounts   special_discountsid = reservationsdiscount_id其中   discount_type = App \ Models \ SpecialDiscount))

如何解决此问题?

0 个答案:

没有答案