mtvs/eloquent-hashids

为 Laravel Eloquent 模型提供即时哈希id。

v3.5.0 2024-03-02 17:32 UTC

This package is auto-updated.

Last update: 2024-09-07 17:02:21 UTC


README

使用哈希id代替url和列表项中的整数id可以更吸引人且更聪明。更多信息请访问 hashids.org

Eloquent-Hashids Build Status

该插件通过即时编码/解码,而不是在数据库中持久化,将哈希id添加到 Laravel Eloquent 模型中。因此不需要额外的数据库列,并且通过在查询中使用主键来提高性能。

功能包括

  • 为模型生成哈希id
  • 解析哈希id到模型
  • 能够为每个模型自定义哈希id设置
  • 使用哈希id进行路由绑定(可选)

安装

使用 Composer 安装包

$ composer require mtvs/eloquent-hashids

此外,发布供应商配置文件到您的应用程序(对于依赖项是必要的)

$ php artisan vendor:publish

设置

通过使用 HasHashid 特性提供基本功能,然后可以使用 HashidRouting 添加路由绑定。

use Illuminate\Database\Eloquent\Model;
use Mtvs\EloquentHashids\HasHashid;
use Mtvs\EloquentHashids\HashidRouting;

Class Item extends Model
{
	use HasHashid, HashidRouting;
}

自定义哈希id设置

通过覆盖 getHashidsConnection(),可以自定义每个模型的哈希id设置。它必须返回提供所需设置的 vinkla/hashids 连接的名称。

用法

基础用法

// Generating the model hashid based on its key
$item->hashid();

// Equivalent to the above but with the attribute style
$item->hashid;

// Finding a model based on the provided hashid or
// returning null on failure
Item::findByHashid($hashid);

// Finding a model based on the provided hashid or
// throwing a ModelNotFoundException on failure
Item::findByHashidOrFail($hashid);

// Decoding a hashid to its equivalent id 
$item->hashidToId($hashid);

// Encoding an id to its equivalent hashid
$item->idToHashid($id);

// Getting the name of the hashid connection
$item->getHashidsConnection();

将哈希id添加到序列化模型中

将其设置为默认值

use Illuminate\Database\Eloquent\Model;
use Mtvs\EloquentHashids\HasHashid;

class Item extends Model
{
    use HasHashid;
    
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['hashid'];
}

或指定特定值

return $item->append('hashid')->toJson();

隐式路由绑定

如果您想使用哈希id值解析模型的隐式路由绑定,可以在模型中使用 HashidRouting

use Illuminate\Database\Eloquent\Model;
use Mtvs\EloquentHashids\HasHashid;
use Mtvs\EloquentHashids\HashidRouting;

class Item extends Model
{
    use HasHashid, HashidRouting;
}

它覆盖了 getRouteKeyName()getRouteKey()resolveRouteBindingQuery() 以使用哈希id作为路由键。

它支持 Laravel 的特定路由自定义键功能。

Route::get('/items/{item:slug}', function (Item $item) {
    return $item;
});

自定义默认路由键名称

如果您想默认使用其他字段解析隐式路由绑定,可以覆盖 getRouteKeyName() 返回字段名称给解析过程,以及 getRouteKey() 返回其在链接中的值。

use Illuminate\Database\Eloquent\Model;
use Mtvs\EloquentHashids\HasHashid;
use Mtvs\EloquentHashids\HashidRouting;

class Item extends Model
{
    use HasHashid, HashidRouting;

    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function getRouteKey()
    {
        return $this->slug;
    }
}

您仍然可以指定特定路由的哈希id。

Route::get('/items/{item:hashid}', function (Item $item) {
    return $item;
});

支持 Laravel 的其他隐式路由绑定功能

当使用 HashidRouting 时,您仍然可以使用软删除和子路由绑定。

Route::get('/items/{item}', function (Item $item) {
    return $item;
})->withTrashed();

Route::get('/user/{user}/items/{item}', function (User $user, Item $item) {
    return $item;
})->scopeBindings();