表结构:
餐厅:
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');
}
}
和SpecialDiscount
和Discount
属于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
内部联接discounts
。id
=reservations
。discount_id
,其中discounts
。deleted_at
为空 和discount_type
= App \ Models \ Discount)联合(选择reservations
。*来自reservations
内部联接special_discounts
special_discounts
。id
=reservations
。discount_id
其中discount_type
= App \ Models \ SpecialDiscount))
如何解决此问题?