corazzi/sluggable 问题修复 & 功能扩展

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

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

corazzi/sluggable

Composer 安装命令:

composer require corazzi/sluggable

包简介

Automatically create slugs for your Eloquent models by hooking into the creating event

README 文档

README

Automatically create slugs for your Eloquent models by hooking into the creating event

Installation

Simply run composer require Corazzi/sluggable in your Laravel project.

Usage

To add automatic slug generation to a model, use the Corazzi\Sluggable\Sluggable trait.

namespace Acme\Models;

use Illuminate\Database\Eloquent\Model;
use Corazzi\Sluggable\Sluggable;

class Post extends Model
{
    use Sluggable;
}

Now when you create a new model that uses the trait, it will look for a name column and generate a new slug, which it will save in the slug column.

$post = Post::create([
    'name' => 'My first post'
]);

$post->slug; // my-first-post

Custom columns

If you would like to use different column names for your origin string and the slug, you can set the protected attributes $slugOrigin and $slugColumn on your model.

namespace Acme\Models;

use Illuminate\Database\Eloquent\Model;
use Corazzi\Sluggable\Sluggable;

class Post extends Model
{
    use Sluggable;
    
    protected $slugOrigin = 'title';
    
    protected $slugColumn = 'post_name';
}

Example

$post = Post::create([
    'title' => 'My second post'
]);

$post->post_name; // my-second-post

Existing slugs

If a slug exists for the model, it will automatically append an integer to the end of the slug until a unique one is generated.

// my-second-post already exists

$post = Post::create([
    'title' => 'My second post'
]);

$post->post_name; // my-second-post-1

$post = Post::create([
    'title' => 'My second post'
]);

$post->post_name; // my-second-post-2

// ...and so on

Explicitly setting slugs

You can explicitly set a slug when creating a new model and the automatic generation will be skipped

$post = Post::create([
    'name' => 'How to explicitly set a slug',
    'slug' => 'explicitly-setting-slugs-with-sluggable'
]);

$post->slug; // explicitly-setting-slugs-with-sluggable

Empty origin

If the origin column is empty, a Corazzi\Sluggable\EmptyOriginException will be thrown.

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固