Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

spatie/freek.dev

636 stars
3 code files
View spatie/freek.dev on GitHub

app/Models/Concerns/HasSlug.php

Open in GitHub
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
 
trait HasSlug
{
public static function bootHasSlug()
{
static::saving(function (Sluggable $model) {
$model->slug = Str::slug($model->getSluggableValue());
});
}
 
public function idSlug(): string
{
return "{$this->id}-{$this->slug}";
}
 
public static function findByIdSlug(string $idSlug): ?Model
{
[$id] = explode('-', $idSlug);
 
return static::find($id);
}
}

app/Models/Link.php

Open in GitHub
use App\Models\Concerns\HasSlug;
use App\Models\Concerns\Sluggable;
use Illuminate\Database\Eloquent\Model;
 
class Link extends Model implements Sluggable
{
use HasSlug;
}

app/Models/Post.php

Open in GitHub
use App\Models\Concerns\HasSlug;
use App\Models\Concerns\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
 
class Post extends Model implements Feedable, Sluggable, HasMedia
{
use HasSlug;
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.