PHP code example of fomvasss / laravel-slugmaker

1. Go to this page and download the library: Download fomvasss/laravel-slugmaker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fomvasss / laravel-slugmaker example snippets


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



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"
    }
}

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

$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']);

$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);

[
    1, 2, 8, 3
]

$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',
    ];

[
    'App\Models\Article' => [1],
    'App\Models\Page' => [2, 8],
    'App\Models\Product' => [3],
]
bash
php artisan vendor:publish --tag=slugmaker-config
bash
php artisan vendor:publish --tag=slugmaker-migrations
bash
php artisan migrate