【发布时间】:2018-07-01 11:32:23
【问题描述】:
您好,我在我的项目中使用 Eloquent 和 Slim。我创建了模型:Reservation、ReservationCottage(链接表)、具有关系的 Cottage:对 N - N 等小屋的预订,现在我想知道如何更改 Eloquent 查询的 SQL 查询。
class Reservation extends Model
{
protected $table = 'reservations';
protected $fillable = [
'start',
'end',
];
public function cottages()
{
return $this->belongsToMany(Cottage::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function reservationStatus()
{
return $this->belongsTo(ReservationStatus::class);
}
public function payments()
{
return $this->hasMany(Payment::class);
}
}
class ReservationCottage extends Model
{
protected $table = 'reservation_cottages';
public function guests()
{
return $this->hasMany(Guest::class);
}
}
class Cottage extends Model
{
protected $table = 'cottages';
protected $fillable = [
'name',
'capacity',
'description',
'base_price',
];
public function additions()
{
return $this->belongsToMany(Addition::class);
}
public function cottagePeriods()
{
return $this->hasMany(CottagePeriod::class);
}
public function periods()
{
return $this->belongsToMany(Period::class);
}
public function reservations()
{
return $this->belongsToMany(Reservation::class);
}
public function cottageStatus()
{
return $this->belongsTo(CottageStatus::class);
}
这是查询预订表中是否有周期(从@ArrivalDate 到@DepartureDate):
SELECT cottage.name FROM cottages WHERE id NOT IN (
SELECT cottage_id
FROM reservation_cottages RC
JOIN reservations R
ON R.id = RC.reservation_id
WHERE (r.start <= @ArrivalDate AND R.end >= @ArrivalDate)
OR (R.start < @DepartureDate AND R.end >= @DepartureDate )
OR (@ArrivalDate <= R.start AND @DepartureDate >= R.start)
【问题讨论】:
-
也许我会改用查询生成器。
-
也许更有用的答案? ;)
标签: php mysql sql laravel eloquent