Lara Reserve Is a Laravel Package To Adds a Reservation feature to the laravel models.
To Install Lara Reserve Run Following Command:
composer require shayanys/lara-reserve
and then run Migrations By:
php artisan migrate
To Add Lara Reserve Feature To Models, Your Models Should Implement ReservableInterface
And use Reservable
Trait.
And the Model Is Ready For Reserve By the Customer. And If Your Model Is a Customer, e.g. User model (Which Can Reserve
Reservables) Should Implement CustomerInterface
And use Customer
Trait.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use ShayanYS\LaraReserve\Interfaces\ReservableInterface;
use ShayanYS\LaraReserve\Models\Reserve;
use ShayanYS\LaraReserve\Traits\Reservable;
class Book extends Model implements ReservableInterface
{
use HasFactory, Reservable;
}
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use ShayanYS\LaraReserve\Interfaces\CustomerInterface;
use ShayanYS\LaraReserve\Traits\Customer;
class User extends Authenticatable implements CustomerInterface
{
use HasApiTokens, HasFactory, Notifiable, Customer;
}
you can reserve a reservable for a customer by reserve method of a customer model:
$reservable = Book::first();
$customer = User::first();
$customer->reserve($reservable,now()->addDay(),'00:00:00',now()->addYear(),'00:00:00',['key' => 'value']);
in the above example, $reservable
will reserve for $customer
.
- reservable you want to reserve for customer
- the desired date for the reservation
- the desired time for the reservation in H:i:s format
- the desired date for the end reservation - optional
- the desired time for the end reservation in H:i:s format - optional
- additional details for the reservation - optional
$reservable = Book::first();
$customer = User::first();
$reservable->reserveForCustomer($customer,now()->addDay(),'00:00:00',now()->addYear(),'00:00:00',['code' => 123]);
In the above example, like the previous example, $reservable
will reserve for $customer
. In the reserveFroCustomer
- the customer would you like to make a reservation for
- the desired date for the reservation
- the desired time for the reservation in H:i:s format
- the desired date for the end reservation - optional
- the desired time for the end reservation in H:i:s format - optional
- additional details for the reservation - optional
$reservable = Book::first();
$reservable->reserveWithoutCustomer(['name' => 'shayan'],now()->addDay(),'00:00:00',now()->addYear(),'00:00:00');
With this method, you can reserve a reservable without a customer.
- array of the reservation details
- the desired date for the reservation
- the desired time for the reservation in H:i:s format
- the desired date for the end reservation - optional
- the desired time for the end reservation in H:i:s format - optional
to set maximum allowed reserve in one date you should add max_allowed_reserves
column to your reservable table in
database:
Schema::table('books', function (Blueprint $table) {
$table->integer('max_allowed_reserves')->nullable();
});
you can set max_allowed_reserves
column of a reservable by calling maxAllowedReserves
from reservable:
this method get $max
to set it as value of max_allowed_reserves
column.
$tableToReserve = ReseturantTable::first();
$tableToReserve->maxAllowedReserves(5);
if you want to get the max_allowed_reserves
from reservable you can do this:
$tableToReserve = ReseturantTable::first();
$tableToReserve->getMaxAllowedReserves();
//or
$tableToReserve->max_allowed_reserves;
returns null if not exists or its null in database.
isAvailable
method can call from reservable and get two arguments date and optional time; and returns is that
reservable available in passed date and time (the time default is 00:00:00).
$airplaneSeat = AirplainSeat::first();
$airplaneSeat->isAvailable(\Carbon\Carbon::createFromFormat('Y-m-d','2023-05-1'),'17:00:00');
This code returns true if max_allowed_reserves
is less than count of all reserves in 2023-05-1 17:00:00; otherwise returns false.
if you don't want to check the availability for some reasons you can use withoutCheckAvailability
method:
$airplaneSeat = AirplainSeat::first();
$customer = User::first();
$airplaneSeat->withoutCheckAvailability()->reserveForCustomer($customer,now()->addDay(),'00:00:00',metadata:['code' => 123]);
//or call reserve method of customer like this:
$customer->reserve($airplaneSeat->withoutCheckAvailability(),now()->addDay(),'00:00:00',metadata:['key' => 'value']);
this code will bypass the check availability.
also you can set reservable to don't check availability by default:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use ShayanYS\LaraReserve\Interfaces\ReservableInterface;
use ShayanYS\LaraReserve\Models\Reserve;
use ShayanYS\LaraReserve\Traits\Reservable;
class AirplaneSeat extends Model implements ReservableInterface
{
use HasFactory,Reservable;
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->checkAvailability = false;
}
}
other way to modify this is to modify shouldCheckAvailability
method:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use ShayanYS\LaraReserve\Interfaces\ReservableInterface;
use ShayanYS\LaraReserve\Models\Reserve;
use ShayanYS\LaraReserve\Traits\Reservable;
class AirplaneSeat extends Model implements ReservableInterface
{
use HasFactory,Reservable;
public function shouldCheckAvailability() : bool{
// TODO: Implement shouldCheckAvailability() method.
return false;
}
}
this will don't check availability by default. if you want check availability when you set checkAvailability
property in constructor to false you should do this:
$airplaneSeat = AirplainSeat::first();
$customer = User::first();
$airplaneSeat->withCheckAvailability()->reserveForCustomer($customer,now()->addDay(),'00:00:00',metadata:['code' => 123]);
//or call reserve method of customer like this:
$customer->reserve($airplaneSeat->withCheckAvailability(),now()->addDay(),'00:00:00',metadata:['key' => 'value']);
this will check availability and then reserve.
you can get reserves from customer or reservable.
$airplaneSeat = AirplainSeat::first();
$customer = User::first();
$airplaneSeat->activeReserves()->get();
// this will return collection of active reserves which reserved this reservable
//(the reservations that have a reserved date and time or end reservation date and time that are greater than or equal to the current date and time.)
$customer->activeReserves()->get();
// this will return collection of active reserves which reserved by this customer
//(the reservations that have a reserved date and time or end reservation date and time that are greater than or equal to the current date and time.)
the activeReserves
method return a MorphMany relation you can call get
method to get the collection of reserves; you can also call paginate
method.
$airplaneSeat = AirplainSeat::first();
$customer = User::first();
$airplaneSeat->allReserves()->get();
// this will return collection of all reserves which reserved this reservable
$customer->allReserves()->get();
// this will return collection of all reserves which reserved by this customer
the allReserves
method return a MorphMany relation you can call get
method to get the collection of reserves; you can also call paginate
method.
$airplaneSeat = AirplainSeat::first();
$customer = User::first();
$airplaneSeat->startedReserves()->get();
// this will return collection of started reserves which reserved this reservable
//(the reservations that have a reserved date and time that are greater than or equal to the current date and time and end reservation date and time less than current date and time.)
$customer->startedReserves()->get();
// this will return collection of started reserves which reserved by this customer
//(the reservations that have a reserved date and time that are greater than or equal to the current date and time and end reservation date and time less than current date and time.)
the startedReserves
method return a MorphMany relation you can call get
method to get the collection of reserves; you can also call paginate
method.
$airplaneSeat = AirplainSeat::first();
$customer = User::first();
$airplaneSeat->endedReserves()->get();
// this will return collection of ended reserves which reserved this reservable
//(the reservations that have a end reservation date and time that are greater than current date and time)
$customer->endedReserves()->get();
// this will return collection of ended reserves which reserved by this customer
//(the reservations that have a end reservation date and time that are greater than current date and time)
the endedReserves
method return a MorphMany relation you can call get
method to get the collection of reserves; you can also call paginate
method.
Freely distributable under the terms of the MIT license.