added slug unique fixer to xcontroller

pull/44/head
A1Gard 6 months ago
parent bb60c111a6
commit 6265c8cf77

@ -225,10 +225,41 @@ abstract class XController extends Controller
public function getSlug($model, $key = 'slug', $name = 'name')
{
if (!\request()->has('slug') || request()->input('slug') == null) {
return sluger($model->$name);
$slug = sluger($model->$name);
} else {
return sluger(\request()->input($key, $model->$name));
$slug = sluger(\request()->input($key, $model->$name));
}
return $this->createUniqueSlug($slug,$model->id);
}
/**
* create unique slug
* @param $slug
* @param $id integer|null
* @return mixed|string
*/
public function createUniqueSlug($slug,$id = null)
{
$originalSlug = $slug;
$counter = 1;
$q = $this->_MODEL_::where('slug', $slug);
if ($id != null){
$q = $q->where('id','<>',$id);
}
while ($q->count() > 0) {
$slug = $originalSlug . '-' . $counter;
$counter++;
$q = $this->_MODEL_::where('slug', $slug);
if ($id != null){
$q = $q->where('id','<>',$id);
}
}
return $slug;
}
}

Loading…
Cancel
Save