-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
167 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str; | ||
|
||
trait HasSlug | ||
{ | ||
public function initializeHasSlug(): void | ||
{ | ||
$this->fillable[] = $this->getSlugColumnName(); | ||
} | ||
|
||
public static function bootHasSlug(): void | ||
{ | ||
static::saving(function (Model $model) { | ||
$model->slug = Str::slug($model->slug); | ||
|
||
if (! $model->slug || ! $model->slugAlreadyUsed($model->slug)) { | ||
$model->slug = $model->generateSlug(); | ||
} | ||
}); | ||
} | ||
|
||
protected function getSlugColumnName(): string | ||
{ | ||
return 'slug'; | ||
} | ||
|
||
protected function getSlugSource(): string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function scopeWhereSlug(Builder $query, string $slug): Builder | ||
{ | ||
return $query->where($this->getSlugColumnName(), $slug); | ||
} | ||
|
||
public function generateSlug(): string | ||
{ | ||
$base = $slug = Str::slug($this->getSlugSource()); | ||
$suffix = 1; | ||
|
||
while ($this->slugAlreadyUsed($slug)) { | ||
$slug = Str::slug($base . '_' . $suffix++); | ||
} | ||
|
||
return $slug; | ||
} | ||
|
||
protected function slugAlreadyUsed(string $slug): bool | ||
{ | ||
$query = static::whereSlug($slug) | ||
->withoutGlobalScopes(); | ||
|
||
if ($this->exists) { | ||
$query->where($this->getKeyName(), '!=', $this->getKey()); | ||
} | ||
|
||
return $query->exists(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\MediaLibrary; | ||
|
||
use Spatie\MediaLibrary\MediaCollections\Models\Media; | ||
use Spatie\MediaLibrary\Support\PathGenerator\DefaultPathGenerator; | ||
|
||
class UserPathGenerator extends DefaultPathGenerator | ||
{ | ||
protected function getBasePath(Media $media): string | ||
{ | ||
return collect([ | ||
'user', | ||
$media->model->ulid, | ||
$media->getKey(), | ||
]) | ||
->filter() | ||
->join(\DIRECTORY_SEPARATOR); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters