Showing
3 changed files
with
22 additions
and
11 deletions
| ... | @@ -61,11 +61,7 @@ class BookController extends Controller | ... | @@ -61,11 +61,7 @@ class BookController extends Controller |
| 61 | 'description' => 'string|max:1000' | 61 | 'description' => 'string|max:1000' |
| 62 | ]); | 62 | ]); |
| 63 | $book = $this->bookRepo->newFromInput($request->all()); | 63 | $book = $this->bookRepo->newFromInput($request->all()); |
| 64 | - $slug = Str::slug($book->name); | 64 | + $book->slug = $this->bookRepo->findSuitableSlug($book->name); |
| 65 | - while($this->bookRepo->countBySlug($slug) > 0) { | ||
| 66 | - $slug .= '1'; | ||
| 67 | - } | ||
| 68 | - $book->slug = $slug; | ||
| 69 | $book->created_by = Auth::user()->id; | 65 | $book->created_by = Auth::user()->id; |
| 70 | $book->updated_by = Auth::user()->id; | 66 | $book->updated_by = Auth::user()->id; |
| 71 | $book->save(); | 67 | $book->save(); |
| ... | @@ -111,11 +107,7 @@ class BookController extends Controller | ... | @@ -111,11 +107,7 @@ class BookController extends Controller |
| 111 | 'description' => 'string|max:1000' | 107 | 'description' => 'string|max:1000' |
| 112 | ]); | 108 | ]); |
| 113 | $book->fill($request->all()); | 109 | $book->fill($request->all()); |
| 114 | - $slug = Str::slug($book->name); | 110 | + $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id); |
| 115 | - while($this->bookRepo->countBySlug($slug) > 0 && $book->slug != $slug) { | ||
| 116 | - $slug += '1'; | ||
| 117 | - } | ||
| 118 | - $book->slug = $slug; | ||
| 119 | $book->updated_by = Auth::user()->id; | 111 | $book->updated_by = Auth::user()->id; |
| 120 | $book->save(); | 112 | $book->save(); |
| 121 | return redirect($book->getUrl()); | 113 | return redirect($book->getUrl()); | ... | ... |
| 1 | <?php namespace Oxbow\Repos; | 1 | <?php namespace Oxbow\Repos; |
| 2 | 2 | ||
| 3 | +use Illuminate\Support\Str; | ||
| 3 | use Oxbow\Book; | 4 | use Oxbow\Book; |
| 4 | 5 | ||
| 5 | class BookRepo | 6 | class BookRepo |
| ... | @@ -62,4 +63,22 @@ class BookRepo | ... | @@ -62,4 +63,22 @@ class BookRepo |
| 62 | return $lastElem ? $lastElem->priority + 1 : 0; | 63 | return $lastElem ? $lastElem->priority + 1 : 0; |
| 63 | } | 64 | } |
| 64 | 65 | ||
| 66 | + public function doesSlugExist($slug, $currentId = false) | ||
| 67 | + { | ||
| 68 | + $query = $this->book->where('slug', '=', $slug); | ||
| 69 | + if($currentId) { | ||
| 70 | + $query = $query->where('id', '!=', $currentId); | ||
| 71 | + } | ||
| 72 | + return $query->count() > 0; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + public function findSuitableSlug($name, $currentId = false) | ||
| 76 | + { | ||
| 77 | + $slug = Str::slug($name); | ||
| 78 | + while($this->doesSlugExist($slug, $currentId)) { | ||
| 79 | + $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); | ||
| 80 | + } | ||
| 81 | + return $slug; | ||
| 82 | + } | ||
| 83 | + | ||
| 65 | } | 84 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -24,7 +24,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon | ... | @@ -24,7 +24,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon |
| 24 | * | 24 | * |
| 25 | * @var array | 25 | * @var array |
| 26 | */ | 26 | */ |
| 27 | - protected $fillable = ['name', 'email', 'password']; | 27 | + protected $fillable = ['name', 'email']; |
| 28 | 28 | ||
| 29 | /** | 29 | /** |
| 30 | * The attributes excluded from the model's JSON form. | 30 | * The attributes excluded from the model's JSON form. | ... | ... |
-
Please register or sign in to post a comment