-
app/Models/Concerns/HasSlug.php
Open in GitHubuse 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 GitHubuse 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 GitHubuse 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; }