larapack/attribute-slugging

允许您的 Eloquent 模型在保存时自动为属性生成唯一的 slug。

安装次数: 1,615

依赖项: 0

建议者: 0

安全: 0

星标: 5

关注者: 2

分支: 1

开放问题: 0

类型:软件包

v1.0.3 2015-12-09 17:31 UTC

This package is auto-updated.

Last update: 2024-09-06 09:42:57 UTC


README

允许您的 Eloquent 模型在保存时自动为属性生成唯一的 slug。

安装

使用 Composer 安装 composer require larapack/attribute-slugging 1.*

用法

首先将特性 Sluggable 添加到您的 Eloquent 模型中。

<?php

namespace App;

use Larapack\AttributeSlugging\Sluggable;

class User
{
  use Sluggable;
  
  /**
   * @var array List of attribute names which should be slugged
   */ 
  protected $slug = [
    'username_slug' => 'username', // set the attribute names you which to slug and from what attribute it should be generated from
  ];
  
  //...
}

测试

// we make two user objects
$userA = new App\User;
$userB = new App\User;
// then we set both username's to "Mark"
$userA->username = "Mark";
$userB->username = "Mark";
// save both objects
$userA->save();
$userB->save();
// now the unique slugs have been set
echo $userA->username_slug; // outputs "mark"
echo $userB->username_slug; // outputs "mark-2"