mgboateng/eloquent-slug 问题修复 & 功能扩展

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

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

mgboateng/eloquent-slug

Composer 安装命令:

composer require mgboateng/eloquent-slug

包简介

Create elegant slugs in Laravel models with ease

README 文档

README

Build Status Total Downloads Latest Stable Version License

The package provides a trait that allows you to save a unique slugs to your database seamlessly by just specifying the seperator, source and destination field to generate a slug. It is very fast and very light on resources as it makes just a single database call when creating a model and two when updating as opposed to looping recurcively over the database to generate a unique slug.

Installation

You can install the package through composer composer require mgboateng/eloquent-slugs or through your composer json file:

{
    "require": {
        "mgboateng/eloquent-slugs" : "~0.2"          
    }
}

Usage

To use the package you simply add the Slugging trait to you model and set protected $slugSettings property as below:

<?php 
namespace App;

use Illuminate\Database\Eloquent\Model;
use MGBoateng\EloquentSlugs\Slugging;

class Post extends Model 
{
    use Slugging;

    protected $slugSettings = [
        'source' => 'title',
        'destination' => 'slug',
        'seperator' => '-'
    ];    
}

The protected $slugSettings array sets

  • The source key which indicate the field to be used as source for making slugs
  • The destination key which specifies the field to store generated slugs
  • The seperator key specifies the seperator to use when generating slugs. eg. '-', '_'

When you are creating a model with a settings as:

protected $slugSettings = [
        'source' => 'title',
        'destination' => 'slug',
        'seperator' => '-'
    ];    

when you craete a model

Post::create([
    'title' => 'Hello World',
    'body' => 'Here comes a great programmer'
]);

an output of

[
    'title' => 'Hello World',
    'slug' => 'hello-world', // or 'hello-world-1' if hello-world already exist
    'body' => 'Here comes a great programmer'
]

will be generated.

You could set the destination field (slug in the above example) to generate a unique slug that is different from the source (title in the above example). When the destination field is directly set it takes precedent over the source field as the source for generating slugs. eg.

Post::create([
    'title' => 'Hello World',
    'slug' => 'Welcome Home'
    'body' => 'Here comes a great programmer'
]);

will output:

[
    'title' => 'Hello World',
    'slug' => 'welcome-home', // or welcome-home-1 if welcome-home already exist
    'body' => 'Here comes a great programmer'
]

License

This software is distributed under the MIT license.

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固