PHP code example of basekit-laravel / basekit-laravel-slugs

1. Go to this page and download the library: Download basekit-laravel/basekit-laravel-slugs 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/ */

    

basekit-laravel / basekit-laravel-slugs example snippets


$page->setSlug('about');
$page->setSlug('acerca-de-nosotros', 'es');

$page->slug();      // 'about'
$page->slug('es');  // 'acerca-de-nosotros'

use BasekitLaravel\BasekitLaravelSlugs\HasSlugs;
use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    use HasSlugs;
}

$page = Page::create(['title' => 'About us']);

$page->setSlug('about');
$page->setSlug('acerca-de-nosotros', 'es');

$page->slug();      // 'about'
$page->slug('es');  // 'acerca-de-nosotros'
$page->slug('de');  // null

$page->hasSlug('es'); // true

$page->slugMap();
// ['en' => 'about', 'es' => 'acerca-de-nosotros']

$page->setSlugFrom($page->title);

// 'About us' -> 'about-us'

$page->setSlugFrom($page->title, 'es');

$generator->generate('España');

// 'espana'

Page::create(['title' => 'About us'])->slug(); // 'about-us'
Page::create(['title' => 'About us'])->slug(); // 'about-us-2'
Page::create(['title' => 'About us'])->slug(); // 'about-us-3'

'disambiguate' => false,

Page::whereSlug('about')->first();
Page::whereSlug('acerca-de-nosotros', 'es')->first();

Page::whereSlugIn(['about', 'pricing'])->get();
Page::whereSlugIn($slugs, 'es')->get();

'auto_slug' => [
    'enabled' => true,
    'attribute' => 'title',
],

$page = Page::create([
    'title' => 'About us',
]);

$page->slug(); // 'about-us'

class Product extends Model
{
    use HasSlugs;

    protected static function slugSourceAttribute(): ?string
    {
        return 'name';
    }
}

class Post extends Model
{
    use HasSlugs;

    protected static function slugSourceAttribute(): ?string
    {
        return null;
    }
}

DB::transaction(
    fn () => Page::create(['title' => 'About us'])
);

use BasekitLaravel\BasekitLaravelSlugs\SlugGenerator;

$generator = app(SlugGenerator::class);

$generator->generate('España');  // 'espana'
$generator->isValid('about-us'); // true

'default_locale' => null,

'disambiguate' => true,

'generator' => [
    'separator' => '-',
    'preserve_unicode' => false,
],

'auto_slug' => [
    'enabled' => false,
    'attribute' => 'title',
],

config('app.locale')

'default_locale' => 'en',

'generator' => [
    'preserve_unicode' => true,
],

'generator' => [
    'separator' => '_',
],

$table->string('sluggable_type');
$table->string('sluggable_id', 26); // 36 for UUID, 26 for ULID

class Article extends Model
{
    use HasUlids;
    use HasSlugs;

    protected static function slugStorageSupportsStringKeys(): bool
    {
        return true;
    }
}

Relation::enforceMorphMap([
    'page' => Page::class,
    'post' => Post::class,
]);

use BasekitLaravel\BasekitLaravelSlugs\Models\Slug;

$page = Page::create(['title' => 'About']);

Slug::factory()
    ->forSluggable($page)
    ->create(['slug' => 'about']);

Slug::factory()
    ->forSluggable($page, 'es')
    ->create(['slug' => 'acerca']);
bash
php artisan vendor:publish --tag="basekit-laravel-slugs-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="basekit-laravel-slugs-config"
text
about_us
about_us_2