naabster/laravel-eloquent-hashids 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

naabster/laravel-eloquent-hashids

最新稳定版本:1.0.4

Composer 安装命令:

composer require naabster/laravel-eloquent-hashids

包简介

Automatically generate hashids in Laravel for a new Eloquent Model.

README 文档

README

Laravel 5 Eloquent Model trait for automatically generating and persistently saving Hashids for new models.
Uses @vinkla's Laravel 5 Hashids package.

Latest Stable Version License

Installation

Require this package, with Composer, in the root directory of your project.

composer require naabster/laravel-eloquent-hashids:~1.0

You should first install @vinkla's Laravel 5 Hashids package according to their installation instructions.

Usage

Use the EloquentHashids trait on your model:

use Illuminate\Database\Eloquent\Model;
use Naabster\EloquentHashids\EloquentHashids;

class Book extends Model
{

  use EloquentHashids;

  // Rest of your eloquent model
}

Configuration

By default the following configuration is used:

Hashids Connection

The default Hashids Connection is table.<tablename>, so for example for the Book model it would be table.books. You can set a different connection for your model with static::getHashidConnection():

use Illuminate\Database\Eloquent\Model;
use Naabster\EloquentHashids\EloquentHashids;

class Book extends Model
{

  use EloquentHashids;

  public static function getHashidConnection(Model $model)
	{
	  return 'someconnection';
	}
}

Make sure you set your connections in your config/hashids.php config file (comes with @vinkla's Laravel 5 Hashids package)!

Column Name

By default the Hashid column is named uid. You can set a different column name for your model with static::getHashidColumn():

use Illuminate\Database\Eloquent\Model;
use Naabster\EloquentHashids\EloquentHashids;

class Book extends Model
{

  use EloquentHashids;

  public static function getHashidColumn(Model $model)
	{
	  return 'hashid';
	}
}

Attention

This package generates Hashids for new models and saves them to a column in your database table! To be able to do that, this column must exist in your table.
You should add this column to your model with a database migration.

Encoding Value

By default the Value/Number for your Hashid is the primary Key attribute of your model (defaults to id). You can adjust that any way you want, just make sure it is Hashids compatible. Just return another value from static::getHashidEncodingValue():

use Illuminate\Database\Eloquent\Model;
use Naabster\EloquentHashids\EloquentHashids;

class Book extends Model
{

  use EloquentHashids;

  public static function getHashidEncodingValue(Model $model)
	{
	  return $model->myId;
	}
}

Example

In this example we generate Hashids with the connection bookHashids, for column hashid and with an encoding value of an array:

use Illuminate\Database\Eloquent\Model;
use Naabster\EloquentHashids\EloquentHashids;

class Book extends Model
{

  use EloquentHashids;

  public static function getHashidConnection(Model $model)
	{
	  return 'bookHashids';
	}
	
  public static function getHashidColumn(Model $model)
	{
	  return 'hashid';
	}

  public static function getHashidEncodingValue(Model $model)
	{
	  return [$model->id, $model->foreign_key];
	}
}

License

The Laravel 5 Eloquent Hashids package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 7.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-29