This commit is contained in:
Kavalar 2024-10-07 11:31:20 +03:00
commit 04da662275
2 changed files with 26 additions and 7 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,29 @@ 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 /**
* @param string $title
* @param $model
* @return 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);
}
/**
* @param $model
* @param string $slug
* @param string $tmpSlug
* @param int $id
* @return string
*/
private 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

@ -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');