简体   繁体   中英

Eloquent representation of SQL query

Hello i'm using Eloquent with Slim in my project. I have created models: Reservation, ReservationCottage(linking table), Cottage with relations : reservations to cottages like N - N, and now i want to know how can i change my SQL query for the Eloquent query.

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);
    }

and this is query wchih check if period(from @ArrivalDate to @DepartureDate ) is available in reservations table :

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)

Ok here is a (hopefully) more helpful answer. In my example, I use the Laravel Query Builder to create such a complex query. Example:

$rows = DB::table('cottages')
    ->select('cottage.name')
    ->from('cottages')
    ->whereNotIn('id', function (Builder $query) {
        $query->select('cottage_id')
            ->from('reservation_cottages AS RC')
            ->join('reservations AS R', 'R.id', '=', 'RC.reservation_id')
            ->whereColumn('r.start', '<=', 'ArrivalDate')
            ->whereColumn('R.end', '>=', 'ArrivalDate')
            ->orWhere(function (Builder $query) {
                $query->whereColumn('R.start', '<', 'DepartureDate')
                    ->whereColumn('R.end', '>=', 'DepartureDate');
            })
            ->orWhere(function (Builder $query) {
                $query->whereColumn('ArrivalDate', '<=', 'R.start')
                    ->whereColumn('DepartureDate', '>=', 'R.start');
            });
    })->get();

Generated SQL:

SELECT 
  `cottage`.`name` 
FROM
  `cottages` 
WHERE `id` NOT IN 
(SELECT 
    `cottage_id` 
    FROM
    `reservation_cottages` AS `RC` 
    INNER JOIN `reservations` AS `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`
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM