aesircloud/sluggable

一个用于自动为模型生成短语的Laravel包

1.0.5 2024-07-11 18:43 UTC

This package is auto-updated.

Last update: 2024-09-11 19:05:15 UTC


README

sluggable 是一个为 Eloquent 模型生成唯一短语的 Laravel 包。它可以在创建或更新模型时自动生成短句。

安装

您可以通过 composer 安装此包

composer require aesircloud/sluggable

发布配置文件

php artisan vendor:publish --provider="AesirCloud\Sluggable\SluggableServiceProvider"

使用方法

要使用此包,请将 Sluggable 特性添加到您的 Eloquent 模型中,并可选地定义 $slugSource 属性以配置短语的生成,默认值是 name

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use AesirCloud\Sluggable\Traits\Sluggable;

class Post extends Model
{
    use Sluggable;

    protected $fillable = ['title', 'slug'];

    protected $slugSource = 'title'; // or 'description', or any other field
}

您需要将一个短句列添加到您的表中。您可以通过创建迁移来实现这一点

php artisan make:migration add_slug_to_posts_table --table=posts
    <?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->string('slug')->unique()->after('title');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropColumn('slug');
        });
    }
};

更新日志

有关最近更改的更多信息,请参阅更新日志

安全

如果您发现有关安全的错误,请通过电子邮件发送到 security@aesircloud.com,而不是使用问题跟踪器。

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件