'description' => 'string|max:1000'
]);
$book = $this->bookRepo->newFromInput($request->all());
- $slug = Str::slug($book->name);
- while($this->bookRepo->countBySlug($slug) > 0) {
- $slug .= '1';
- }
- $book->slug = $slug;
+ $book->slug = $this->bookRepo->findSuitableSlug($book->name);
$book->created_by = Auth::user()->id;
$book->updated_by = Auth::user()->id;
$book->save();
'description' => 'string|max:1000'
]);
$book->fill($request->all());
- $slug = Str::slug($book->name);
- while($this->bookRepo->countBySlug($slug) > 0 && $book->slug != $slug) {
- $slug += '1';
- }
- $book->slug = $slug;
+ $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
$book->updated_by = Auth::user()->id;
$book->save();
return redirect($book->getUrl());
<?php namespace Oxbow\Repos;
+use Illuminate\Support\Str;
use Oxbow\Book;
class BookRepo
return $lastElem ? $lastElem->priority + 1 : 0;
}
+ public function doesSlugExist($slug, $currentId = false)
+ {
+ $query = $this->book->where('slug', '=', $slug);
+ if($currentId) {
+ $query = $query->where('id', '!=', $currentId);
+ }
+ return $query->count() > 0;
+ }
+
+ public function findSuitableSlug($name, $currentId = false)
+ {
+ $slug = Str::slug($name);
+ while($this->doesSlugExist($slug, $currentId)) {
+ $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
+ }
+ return $slug;
+ }
+
}
\ No newline at end of file
*
* @var array
*/
- protected $fillable = ['name', 'email', 'password'];
+ protected $fillable = ['name', 'email'];
/**
* The attributes excluded from the model's JSON form.