Skip to content

Instantly share code, notes, and snippets.

@logaretm
Created January 2, 2017 06:53
Show Gist options
  • Select an option

  • Save logaretm/fa7bd58b1ae8adcb9d102cc7a6ba3518 to your computer and use it in GitHub Desktop.

Select an option

Save logaretm/fa7bd58b1ae8adcb9d102cc7a6ba3518 to your computer and use it in GitHub Desktop.

Revisions

  1. logaretm created this gist Jan 2, 2017.
    30 changes: 30 additions & 0 deletions Sluggable.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <?php

    namespace App\Traits;

    trait Sluggable
    {
    /**
    * Boots the sluggable trait.
    */
    public static function bootSluggable()
    {
    static::saving(function ($model) {
    if (! $model->exists) {
    $model->slug = str_slug($model->{$model->getSluggableSource()});
    }
    });
    }

    /**
    * @return string
    */
    protected function getSluggableSource()
    {
    if (property_exists($this, "sluggableSource")) {
    return $this->{"sluggableSource"};
    }

    return "name";
    }
    }