Yanselmask / reservable
一个用于预约的包
Requires
- php: ^8.1
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.1
This package is auto-updated.
Last update: 2024-09-06 23:57:01 UTC
README
Yanselmask Reservable 是一个 Laravel 扩展包,为 Laravel 模型添加预约功能。
安装
要安装 Yanselmask Reservable,请运行以下命令:
composer require yanselmask/reservable
然后运行迁移:
php artisan migrate
用法
初始化模型以使用 Yanselmask Reservable
要将 Yanselmask Reservable 功能添加到模型,您的模型应实现 ReservableInterface
并使用 Reservable
特性。模型现在可以由客户预约。如果您的模型是客户,例如用户模型(可以预约可预约项),则应实现 CustomerInterface
并使用 Customer
特性。
示例
可预约模型
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphMany; use Yanselmask\Reservable\Interfaces\ReservableInterface; use Yanselmask\Reservable\Models\Reserve; use Yanselmask\Reservable\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 Yanselmask\Reservable\Interfaces\CustomerInterface; use Yanselmask\Reservable\Traits\Customer; class User extends Authenticatable implements CustomerInterface { use HasApiTokens, HasFactory, Notifiable, Customer; }
预约
从客户调用 reserve
方法
您可以通过客户模型的预约方法为客户预约可预约项
$reservable = Book::first(); $customer = User::first(); $customer->reserve($reservable,new Datatime('2024-08-07'),'00:00:00',new Datatime('2024-08-08'),'00:00:00',['key' => 'value']);
在上面的示例中,$reservable
将为 $customer
进行预约。
参数
- 您想为客户预约的可预约项
- 预约的期望日期
- 预约的期望时间(H:i:s 格式)
- 预约结束的期望日期 - 可选
- 预约结束的期望时间(H:i:s 格式) - 可选
- 预约的附加详细信息 - 可选
从可预约项调用 reserveForCustomer
方法
$reservable = Book::first(); $customer = User::first(); $reservable->reserveForCustomer($customer,new Datatime('2024-08-07'),'00:00:00',new Datatime('2024-08-08'),'00:00:00',['code' => 123]);
在上面的示例中,与前面的示例类似,$reservable
将为 $customer
进行预约。在 reserveFroCustomer
参数
- 您想为其预约的客户
- 预约的期望日期
- 预约的期望时间(H:i:s 格式)
- 预约结束的期望日期 - 可选
- 预约结束的期望时间(H:i:s 格式) - 可选
- 预约的附加详细信息 - 可选
从可预约项调用 reserveWithoutCustomer
方法
$reservable = Book::first(); $reservable->reserveWithoutCustomer(['name' => 'yanselmask'],new Datatime('2024-08-07'),'00:00:00',new Datatime('2024-08-08'),'00:00:00');
使用此方法,您可以在没有客户的情况下预约可预约项。
参数
- 预约详细信息的数组
- 预约的期望日期
- 预约的期望时间(H:i:s 格式)
- 预约结束的期望日期 - 可选
- 预约结束的期望时间(H:i:s 格式) - 可选
最大允许预约次数
设置一个日期和时间的最大可能预约次数。
要设置一个日期的最大允许预约次数,您应该在数据库中的可预约项表中添加 max_allowed_reserves
列
Schema::table('books', function (Blueprint $table) { // You can usage that $table->maxAllowedReserves(); // or that $table->integer('max_allowed_reserves')->nullable(); });
您可以通过从可预约项调用 maxAllowedReserves
来设置可预约项的 max_allowed_reserves
列:此方法将 $max
设置为 max_allowed_reserves
列的值。
$tableToReserve = ReseturantTable::first(); $tableToReserve->maxAllowedReserves(5);
如果您想从可预约项获取 max_allowed_reserves
,您可以这样做
$tableToReserve = ReseturantTable::first(); $tableToReserve->getMaxAllowedReserves(); //or $tableToReserve->max_allowed_reserves;
如果不存在或其在数据库中为空,则返回 null。
是否可用
isAvailable
方法可以从可预约项调用,并接收两个参数日期和可选时间;然后返回该可预约项在指定日期和时间(默认时间为 00:00:00)是否可用。
$airplaneSeat = AirplainSeat::first(); $airplaneSeat->isAvailable(\Carbon\Carbon::createFromFormat('Y-m-d','2023-05-1'),'17:00:00');
如果 max_allowed_reserves
小于 2023-05-01 17:00:00 的所有预约数,则此代码返回 true;否则返回 false。
withoutCheckAvailability 和 withCheckAvailability
如果您出于某些原因不想检查可用性,则可以使用 withoutCheckAvailability
方法
$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']);
此代码将跳过检查可用性。
您还可以将可预约项设置为默认不检查可用性
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphMany; use Yanselmask\Reservable\Interfaces\ReservableInterface; use Yanselmask\Reservable\Models\Reserve; use Yanselmask\Reservable\Traits\Reservable; class AirplaneSeat extends Model implements ReservableInterface { use HasFactory,Reservable; public function __construct(array $attributes = []) { parent::__construct($attributes); $this->checkAvailability = false; } }
修改此设置的另一种方法是修改 shouldCheckAvailability
方法
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphMany; use Yanselmask\Reservable\Interfaces\ReservableInterface; use Yanselmask\Reservable\Models\Reserve; use Yanselmask\Reservable\Traits\Reservable; class AirplaneSeat extends Model implements ReservableInterface { use HasFactory,Reservable; public function shouldCheckAvailability() : bool{ // TODO: Implement shouldCheckAvailability() method. return false; } }
这将默认不检查可用性。如果您想在构造函数中将 checkAvailability
属性设置为 false 来检查可用性,则应这样做
$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']);
这将检查可用性然后进行预约。
获取预约
您可以从客户或可预约项中获取预约。
activeReserves
$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.)
activeReserves
方法返回一个 MorphMany 关系,您可以调用 get
方法获取储备金的集合;您还可以调用 paginate
方法。
allReserves
$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
allReserves
方法返回一个 MorphMany 关系,您可以调用 get
方法获取储备金的集合;您还可以调用 paginate
方法。
startedReserves
$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.)
startedReserves
方法返回一个 MorphMany 关系,您可以调用 get
方法获取储备金的集合;您还可以调用 paginate
方法。
endedReserves
$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)
endedReserves
方法返回一个 MorphMany 关系,您可以调用 get
方法获取储备金的集合;您还可以调用 paginate
方法。
版权信息
Yanselmask Reservable 基于 shayan-yousefi/lara-reserve
。所有版权归原作者所有。
许可证
在 MIT 许可证条款下自由分发。