|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Retosteffen\LaravelBlog\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Retosteffen\LaravelBlog\Models\Category; |
|
7
|
|
|
use Retosteffen\LaravelBlog\Models\LaravelBlog; |
|
8
|
|
|
|
|
9
|
|
|
class CategoryController |
|
10
|
|
|
{ |
|
11
|
|
|
public function index(Request $request) |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
$categories = Category::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE); |
|
14
|
|
|
|
|
15
|
|
|
return view('laravel-blog::category.index', compact('categories')); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function create(Request $request) |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
return view('laravel-blog::category.create'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function store(Request $request) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
$attributes = request()->validate([ |
|
26
|
|
|
'name'=>['required'], |
|
27
|
|
|
]); |
|
28
|
|
|
|
|
29
|
|
|
$category = Category::create($attributes); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
return redirect()->route('categories'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
View Code Duplication |
public function show(Category $category) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$item = $category; |
|
37
|
|
|
$posts = LaravelBlog::where('published', '=', true)->where('category_id', '=', $item->id)->orderBy('published_at', 'desc')->get(); |
|
38
|
|
|
|
|
39
|
|
|
return view('laravel-blog::archive', compact('item', 'posts')); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function edit(Category $category) |
|
43
|
|
|
{ |
|
44
|
|
|
return view('laravel-blog::category.edit', compact('category')); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
View Code Duplication |
public function update(Request $request, Category $category) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$attributes = request()->validate([ |
|
50
|
|
|
'name'=>['required'], |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
$category->update($attributes); |
|
54
|
|
|
|
|
55
|
|
|
return redirect()->route('categories'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function destroy(Category $category) |
|
59
|
|
|
{ |
|
60
|
|
|
$category->delete(); |
|
61
|
|
|
|
|
62
|
|
|
return redirect()->route('categories'); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.