shayanys/lara-reserve

一个用于预订的包

安装量: 1,697

依赖关系: 0

建议者: 0

安全: 0

星标: 79

关注者: 6

分支: 4

开放性问题: 1

类型:laravel-package

2.0.0 2023-05-06 13:21 UTC

This package is auto-updated.

Last update: 2024-09-06 17:59:11 UTC


README

Lara Reserve license Lara Reserve size Lara Reserve version

Lara Reserve 是一个 Laravel 包,用于为 Laravel 模型添加预订功能。

安装

要安装 Lara Reserve,请运行以下命令

composer require shayanys/lara-reserve

然后通过以下命令运行迁移

php artisan migrate

用法

初始化模型以使用 Lara Reserve

要将 Lara Reserve 功能添加到模型,您的模型应实现 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 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;

}

预订

从客户调用 reserve 方法

您可以通过客户模型的预订方法为客户预订可预订项

$reservable = Book::first();
$customer = User::first();

$customer->reserve($reservable,now()->addDay(),'00:00:00',now()->addYear(),'00:00:00',['key' => 'value']);

在上面的示例中,$reservable 将为 $customer 预订。

参数

  1. 您想为客户预订的可预订项
  2. 预订期望的日期
  3. 预订期望的时间,格式为 H:i:s
  4. 预订结束日期 - 可选
  5. 预订结束时间,格式为 H:i:s - 可选
  6. 预订的附加详细信息 - 可选

从可预订项调用 reserveForCustomer 方法

$reservable = Book::first();
$customer = User::first();

$reservable->reserveForCustomer($customer,now()->addDay(),'00:00:00',now()->addYear(),'00:00:00',['code' => 123]);

在上面的示例中,与前面的示例类似,$reservable 将为 $customer 预订。在 reserveFroCustomer

参数

  1. 您想为其预订的客户
  2. 预订期望的日期
  3. 预订期望的时间,格式为 H:i:s
  4. 预订结束日期 - 可选
  5. 预订结束时间,格式为 H:i:s - 可选
  6. 预订的附加详细信息 - 可选

从可预订项调用 reserveWithoutCustomer 方法

$reservable = Book::first();

$reservable->reserveWithoutCustomer(['name' => 'shayan'],now()->addDay(),'00:00:00',now()->addYear(),'00:00:00');

使用此方法,您可以在没有客户的情况下预订可预订项。

参数

  1. 预订详细信息的数组
  2. 预订期望的日期
  3. 预订期望的时间,格式为 H:i:s
  4. 预订结束日期 - 可选
  5. 预订结束时间,格式为 H:i:s - 可选

最大允许预订数

设置一个日期和时间的最大可能预订数。

要设置一个日期的最大允许预订数,您应该在数据库中的可预订表中添加 max_allowed_reserves

Schema::table('books', function (Blueprint $table) {
    $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');

此代码返回 true,如果 max_allowed_reserves 小于 2023-05-01 17:00:00 的所有预订数;否则返回 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 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;
    }
}

另一种修改方法是通过修改 shouldCheckAvailability 方法

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

这将默认不检查可用性。如果您想在设置 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方法。

许可证

在MIT许可证条款下自由分发。