diff --git a/kernel/helpers/Slug.php b/kernel/helpers/Slug.php index 423e37e..aab7264 100644 --- a/kernel/helpers/Slug.php +++ b/kernel/helpers/Slug.php @@ -2,6 +2,7 @@ namespace kernel\helpers; +use Illuminate\Database\Eloquent\Model; use kernel\FormModel; use kernel\modules\post\models\Post; @@ -126,7 +127,17 @@ class Slug return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str; } - public static function recursiveCreateSlug($model, $slug, $tmpSlug = '', $id = 1): string + public static function createSlug(string $title, $model = null): string + { + $slug = Slug::url_slug($title, ['transliterate' => true, 'lowercase' => true]); + if ($model === null) { + return $slug; + } + + return Slug::recursiveCreateSlug($model, $slug); + } + + protected static function recursiveCreateSlug($model, string $slug, string $tmpSlug = '', int $id = 1): string { if ($tmpSlug === '') $tmpSlug = $slug; if ($model::where(['slug' => $tmpSlug])->exists()) { diff --git a/kernel/modules/post/models/Post.php b/kernel/modules/post/models/Post.php index b3482d1..76dc634 100644 --- a/kernel/modules/post/models/Post.php +++ b/kernel/modules/post/models/Post.php @@ -9,7 +9,6 @@ use kernel\modules\user\models\User; * @property string $content * @property string $title * @property string $slug -// * @property string $username * @property int $user_id * @method static where(int[] $array) * @method static find($id) diff --git a/kernel/modules/post/service/PostService.php b/kernel/modules/post/service/PostService.php index 764b6b7..7b3abad 100644 --- a/kernel/modules/post/service/PostService.php +++ b/kernel/modules/post/service/PostService.php @@ -15,7 +15,8 @@ class PostService $model->content = $form_model->getItem('content'); $model->user_id = $form_model->getItem('user_id'); $model->title = $form_model->getItem('title'); - $model->slug = Slug::recursiveCreateSlug(Post::class, $this->createSlug($form_model)); +// $model->slug = Slug::recursiveCreateSlug(Post::class, $this->createSlug($form_model)); + $model->slug = Slug::createSlug($form_model->getItem('title'), Post::class); if ($model->save()){ return $model; } @@ -23,11 +24,6 @@ class PostService return false; } - public function createSlug(FormModel $form_model): string - { - return Slug::url_slug($form_model->getItem('title'), ['transliterate' => true, 'lowercase' => true]); - } - public function update(FormModel $form_model, Post $post): false|Post { $post->content = $form_model->getItem('content');