2017 © Pedro Peláez
 

library laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

image

fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  • Monday, April 16, 2018
  • by fomvasss
  • Repository
  • 1 Watchers
  • 1 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Generate slugs in a separate table when saving Eloquent models

Latest Version on Packagist Software License Quality Score StyleCI Total Downloads, (*1)

This package provides a trait that will generate in a separate table a unique slug when saving any Eloquent model., (*2)

The slugs are generated with Laravels str_slug method, whereby spaces are converted to '-'., (*3)

Installation

You can install the package via composer: ``` bash composer require fomvasss/laravel-slugmaker, (*4)

---
! For Laravel < v5.5

Add the ServiceProvider to the providers array in config/app.php:

```bash
Fomvasss\SlugMaker\SlugMakerServiceProvider::class,

Publish config file:, (*5)

php artisan vendor:publish --tag=slugmaker-config

Publish the migration file:, (*6)

php artisan vendor:publish --tag=slugmaker-migrations

and run the migration:, (*7)

php artisan migrate

Usage

Configure

! Pay attention! Your model should not have a field named "slug", (*8)

Your Eloquent models should use the Fomvasss\SlugMaker\ModelHasSlug trait., (*9)

The trait contains an method getSlugSourceFields() that you must implement yourself., (*10)

Also the trait contains public method slug() for relations your item-model with item-slug:, (*11)

public function slug()
{
    return $this->morphOne(\Fomvasss\SlugMaker\Models\Slug::class, 'slugable');
}

Here's an example of how to implement the trait:, (*12)

<?php

namespace App\Models;

use Fomvasss\SlugMaker\ModelHasSlug;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use ModelHasSlug;

    protected $with = ['slug'];

    /**
     * Get the default fields for generating the slug.
     */
    public function getSlugSourceFields()
    {
        return ['id', 'name', 'created_at']; // "124-article-test-slug-2017-12-26-135705"
    }
}

Get & make slugs

You can to get slug with relation-method slug():, (*13)

$article = Article::find(13);
$slug = $article->slug->name;

Or use next scope-methods in your models: - getSlugName() - findBySlug($slug) - findOrFailBySlug($slug) - getBySlugs(array $slugs) - getArrayIdsBySlugs(array $slugs) - makeSlug(string $str = ''), (*14)

For example:, (*15)

$slug = Article::find(13)->getSlugName();
$slug = Article::findBySlug('my-slug');
$slug = Article::getBySlugs(['my-slug-1', 'my-slug-2']);
$slug = Article::getArrayIdsBySlugs(['my-slug-1', 'my-slug-2']);

Usage function helpers:

  • slug_get($slug)
  • slug_get_models($slugs, $class = null)
  • slug_get_id($slug, $class = null)
  • slug_get_ids($slugs, $class = null)
  • slug_get_grouped_class($attributes)
  • slug_make($model, $slug = '') // if empty $slug - generate with getSlugSourceFields(): array
  • slug_delete($model)

Usage class SlugHelper:

$helper = new Fomvasss\SlugMaker\SlugHelper();
$helper->getModel($slug, $modelClass = null);
$helper->getModels(array $slugs, $modelClass = null);
$helper->getId($slug, $modelClass = null);
$helper->getIds(array $slugs, $modelClass = null);
$helper->getIdsGroupedByClass(array $attributes);
$helper->makeForModel($model, $slug = '');
$helper->deleteByModel($model);

The getIds() return the array:, (*16)

[
    1, 2, 8, 3
]

```php $attributes = [ App\Models\Article::class => ['slug-article-name',], App\Models\Page::class => ['slug-page-name-1', 'slug-page-name-2'], App\Models\Product::class => 'slug-product-name', ];, (*17)

The `getIdsGroupedByClass($attributes)` return the array:
```php
[
    'App\Models\Article' => [1],
    'App\Models\Page' => [2, 8],
    'App\Models\Product' => [3],
]

Change log

Please see CHANGELOG for more information what has changed recently., (*18)

Security

If you discover any security related issues, please email fomvasss@gmail.com instead of using the issue tracker., (*19)

Credits

License

The MIT License (MIT). Please see License File for more information., (*20)

The Versions

16/04 2018

dev-master

9999999-dev https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

16/04 2018

1.1.2

1.1.2.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

13/04 2018

1.1.1

1.1.1.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

28/01 2018

1.1.0

1.1.0.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

11/01 2018

1.0.1

1.0.1.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

26/12 2017

1.5.0

1.5.0.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

22/12 2017

1.4.1

1.4.1.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

21/12 2017

1.4.0

1.4.0.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

06/12 2017

1.3.0

1.3.0.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

04/12 2017

1.2.0

1.2.0.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker

02/12 2017

1.0.0

1.0.0.0 https://github.com/fomvasss/laravel-slugmaker

Generate slugs when saving Eloquent models in a separate table

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

slug fomvasss laravel-slugmaker