Skip to content

Commit 0775cef

Browse files
committed
Use slugs
1 parent bff6423 commit 0775cef

File tree

11 files changed

+32
-26
lines changed

11 files changed

+32
-26
lines changed

app/Http/Controllers/Articles/SeriesController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function create()
2929
$tags = Tag::all();
3030
$selectedTags = old('tags') ?: [];
3131

32-
return view('articles.series.create', ['tags' => $tags, 'selectedTags' => $selectedTags]);
32+
return view('series.create', ['tags' => $tags, 'selectedTags' => $selectedTags]);
3333
}
3434

3535
public function store(SeriesRequest $request)
@@ -38,15 +38,15 @@ public function store(SeriesRequest $request)
3838

3939
$this->success('series.created');
4040

41-
return redirect()->route('series.show', $series->id());
41+
return redirect()->route('series.show', $series->slug());
4242
}
4343

4444
public function edit(Series $series)
4545
{
4646
$this->authorize(SeriesPolicy::UPDATE, $series);
4747
$selectedTags = $series->tags()->pluck('id')->toArray();
4848

49-
return view('articles.series.edit', ['series' => $series, 'tags' => Tag::all(), 'selectedTags' => $selectedTags]);
49+
return view('series.edit', ['series' => $series, 'tags' => Tag::all(), 'selectedTags' => $selectedTags]);
5050
}
5151

5252
public function update(SeriesRequest $request, Series $series)
@@ -57,6 +57,6 @@ public function update(SeriesRequest $request, Series $series)
5757

5858
$this->success('series.updated');
5959

60-
return redirect()->route('series.show', $series->id());
60+
return redirect()->route('series.show', $series->slug());
6161
}
6262
}

app/Jobs/CreateSeries.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function handle(): Series
4343
{
4444
$series = new Series([
4545
'title' => $this->title,
46+
'slug' => $this->title,
4647
]);
4748
$series->authoredBy($this->author);
4849
$series->syncTags($this->tags);

app/Jobs/UpdateSeries.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ final class UpdateSeries
1515
public function __construct(Series $series, array $attributes = [])
1616
{
1717
$this->series = $series;
18-
$this->attributes = Arr::only($attributes, ['title', 'tags']);
18+
$this->attributes = Arr::only($attributes, ['title', 'slug', 'tags']);
1919
}
2020

2121
public static function fromRequest(Series $series, SeriesRequest $request): self
2222
{
2323
return new static($series, [
2424
'title' => $request->title(),
25+
'slug' => $request->title(),
2526
'tags' => $request->tags(),
2627
]);
2728
}

app/Models/Series.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
namespace App\Models;
44

55
use App\Helpers\HasAuthor;
6+
use App\Helpers\HasSlug;
67
use App\Helpers\HasTags;
78
use Illuminate\Database\Eloquent\Model;
89

910
class Series extends Model
1011
{
11-
use HasAuthor, HasTags;
12+
use HasAuthor, HasTags, HasSlug;
1213

1314
/**
1415
* {@inheritdoc}
1516
*/
1617
protected $fillable = [
1718
'title',
19+
'slug',
1820
];
1921

2022
public function id(): int

database/migrations/2020_04_07_181731_create_series_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function up()
1717
$table->increments('id');
1818
$table->unsignedInteger('author_id');
1919
$table->string('title');
20+
$table->string('slug')->unique();
2021
$table->timestamps();
2122

2223
$table->foreign('author_id')

routes/bindings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
Route::bind('article', function (string $slug) {
2525
return App\Models\Article::findBySlug($slug);
2626
});
27-
Route::bind('series', function (string $id) {
28-
return App\Models\Series::findOrFail($id);
27+
Route::bind('series', function (string $slug) {
28+
return App\Models\Series::findBySlug($slug);
2929
});

routes/web.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@
7979

8080
// Articles
8181
Route::prefix('articles')->namespace('Articles')->group(function () {
82-
// Series
83-
Route::prefix('series')->group(function () {
84-
Route::get('/create', 'SeriesController@create')->name('series.create');
85-
Route::post('/', 'SeriesController@store')->name('series.store');
86-
Route::get('{series}', 'SeriesController@show')->name('series.show');
87-
Route::get('{series}/edit', 'SeriesController@edit')->name('series.edit');
88-
Route::put('{series}', 'SeriesController@update')->name('series.update');
89-
});
90-
9182
Route::get('/', 'ArticlesController@index')->name('articles');
9283
Route::get('/create', 'ArticlesController@create')->name('articles.create');
9384
Route::post('/', 'ArticlesController@store')->name('articles.store');
@@ -97,6 +88,15 @@
9788
Route::delete('{article}', 'ArticlesController@delete')->name('articles.delete');
9889
});
9990

91+
// Series
92+
Route::prefix('series')->namespace('Articles')->group(function () {
93+
Route::get('/create', 'SeriesController@create')->name('series.create');
94+
Route::post('/', 'SeriesController@store')->name('series.store');
95+
Route::get('{series}', 'SeriesController@show')->name('series.show');
96+
Route::get('{series}/edit', 'SeriesController@edit')->name('series.edit');
97+
Route::put('{series}', 'SeriesController@update')->name('series.update');
98+
});
99+
100100
// Admin
101101
Route::prefix('admin')->name('admin')->namespace('Admin')->group(function () {
102102
Route::get('/', 'AdminController@index');

0 commit comments

Comments
 (0)