承接 mvnrsa/laravel-sluggable-trait 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

mvnrsa/laravel-sluggable-trait

最新稳定版本:0.3.2

Composer 安装命令:

composer require mvnrsa/laravel-sluggable-trait

包简介

A trait you can apply to Eloquent models to have slugs automatically generated on save. A slight omprovement on martinbean/laravel-sluggable-trait to use underscore (or any other character) instead of dash in slugs.

README 文档

README

A trait you can apply to Eloquent models to have slugs automatically generated on save.
This is a slight improvement on martinbean/laravel-sluggable-trait to allow the slug character to be changed from dash to underscore (by default) or any character of your choice.

Installation

$ composer require mvnrsa/laravel-sluggable-trait

Usage

<?php

namespace App;

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

class Item extends Model
{
    use Sluggable;
}

By default, the trait assumes your database has two columns: name and slug. If you need to change these, you can override the getter methods:

<?php

namespace App;

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

class Item extends Model
{
    use Sluggable;

    public function getSlugColumnName()
    {
        return 'seo_title';
    }

    public function getSluggableString()
    {
        return 'headline';
    }

    /* Optional */
    public function getSlugCharacter()
    {
        return '_';
    }
}

The getSlugColumnName() method should return the name of the column you want to store slugs in your database table.

The getSluggableString() should return a string you want to create a slug from. This is exposed as a method and not a property or constantly as you may want to create a slug from the value of one than one column. For example:

/**
 * Create a string based on the first and last name of a person.
 */
public function getSluggableString()
{
    return sprintf('%s %s', $this->first_name, $this->last_name);
}
/**
 * Create a string based on a formatted address string.
 */
public function getSluggableString()
{
    return implode(', ', array_filter([
        $this->street_address,
        $this->locality,
        $this->region,
        $this->postal_code,
        $this->country,
    ]));
}

The getSlugCharacter() should return the character you want to use as replacement in slugs.
My default is an underscore (_).

/**
 * Allows overriding the character used in slugs.
 */
public function getSlugCharacter()
{
	return '_';
}

License

Licensed under the MIT Licence.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-01