alzpk/laraslug

Small package that it easy to create slug for laravel models

Installs: 3

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:package

1.0.0 2022-04-26 18:29 UTC

This package is auto-updated.

Last update: 2024-10-27 00:28:15 UTC


README

Small package that makes it easy to create slug for laravel models

Installation

To install this package, run the following composer command, inside your laravel project.

composer require alzpk/laraslug

Usage

To use this package simply use Alzpk\Laraslug inside your model.

Example:

namespace App\Models;

use Alzpk\Laraslug\Laraslug;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Laraslug;
}

The example above expects your model to have at least one of each columns listed below:

Slug column (the column name, which should hold the slug value):

  • slug

Slug value column (the column name, which holds the value that should be slugged):

  • title
  • name

Advanced usage

If you want to customize one or more of the columns, and perhaps want a prefix for the slug, you can make use of these variables, inside your model:

  • Custom slug column: private string $slugColumn
  • Custom slug value column: private string $slugValueColumn
  • Add prefix to the slug: private string $slugPrefix

Example:

Here we have an example of a migration and model, called Post that has the columns subject and uri.

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('subject');
            $table->string('uri');
            $table->timestamps();
        });
    }

To make the slug work now we simply have to add private string $slugColumn and private string $slugValueColumn to the model.

namespace App\Models;

use Alzpk\Laraslug\Laraslug;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Laraslug;
    
    private string $slugColumn = 'uri';
    private string $slugValueColumn = 'subject';
}

Prefix:

If we want to add a prefix to our slug, we just need to add private string $slugPrefix to our model.

namespace App\Models;

use Alzpk\Laraslug\Laraslug;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Laraslug;
    
    private string $slugPrefix = 'prefix';
}

This would then make a slug like so "prefix-VALUE".