Create slug

This commit is contained in:
Билай Станислав 2024-10-01 16:00:14 +03:00
parent 160c381abc
commit 5944dac053
3 changed files with 14 additions and 8 deletions

View File

@ -2,6 +2,7 @@
namespace kernel\helpers; namespace kernel\helpers;
use Illuminate\Database\Eloquent\Model;
use kernel\FormModel; use kernel\FormModel;
use kernel\modules\post\models\Post; use kernel\modules\post\models\Post;
@ -126,7 +127,17 @@ class Slug
return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str; 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 ($tmpSlug === '') $tmpSlug = $slug;
if ($model::where(['slug' => $tmpSlug])->exists()) { if ($model::where(['slug' => $tmpSlug])->exists()) {

View File

@ -9,7 +9,6 @@ use kernel\modules\user\models\User;
* @property string $content * @property string $content
* @property string $title * @property string $title
* @property string $slug * @property string $slug
// * @property string $username
* @property int $user_id * @property int $user_id
* @method static where(int[] $array) * @method static where(int[] $array)
* @method static find($id) * @method static find($id)

View File

@ -15,7 +15,8 @@ class PostService
$model->content = $form_model->getItem('content'); $model->content = $form_model->getItem('content');
$model->user_id = $form_model->getItem('user_id'); $model->user_id = $form_model->getItem('user_id');
$model->title = $form_model->getItem('title'); $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()){ if ($model->save()){
return $model; return $model;
} }
@ -23,11 +24,6 @@ class PostService
return false; 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 public function update(FormModel $form_model, Post $post): false|Post
{ {
$post->content = $form_model->getItem('content'); $post->content = $form_model->getItem('content');