|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelSmartBlog\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Validator; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Support\Facades\DB; |
|
9
|
|
|
use DavideCasiraghi\LaravelSmartBlog\Models\Category; |
|
10
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
|
11
|
|
|
|
|
12
|
|
|
class CategoryController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
/* Restrict the access to this resource just to logged in users */ |
|
15
|
11 |
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
11 |
|
$this->middleware('admin'); |
|
18
|
11 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Display a listing of the resource. |
|
22
|
|
|
* |
|
23
|
|
|
* @return \Illuminate\Http\Response |
|
24
|
|
|
*/ |
|
25
|
5 |
|
public function index() |
|
26
|
|
|
{ |
|
27
|
5 |
|
$categories = Category::latest()->paginate(10); |
|
28
|
|
|
|
|
29
|
|
|
// Countries available for translations |
|
30
|
5 |
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
|
31
|
|
|
|
|
32
|
5 |
|
return view('laravel-smart-blog::categories.index', compact('categories')) |
|
33
|
5 |
|
->with('i', (request()->input('page', 1) - 1) * 10) |
|
34
|
5 |
|
->with('countriesAvailableForTranslations', $countriesAvailableForTranslations); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Show the form for creating a new resource. |
|
39
|
|
|
* |
|
40
|
|
|
* @return \Illuminate\Http\Response |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function create() |
|
43
|
|
|
{ |
|
44
|
1 |
|
return view('laravel-smart-blog::categories.create'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Store a newly created resource in storage. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \Illuminate\Http\Request $request |
|
51
|
|
|
* @return \Illuminate\Http\Response |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function store(Request $request) |
|
54
|
|
|
{ |
|
55
|
|
|
// Validate form datas |
|
56
|
2 |
|
$validator = Validator::make($request->all(), [ |
|
57
|
2 |
|
'name' => 'required', |
|
58
|
|
|
]); |
|
59
|
2 |
|
if ($validator->fails()) { |
|
60
|
1 |
|
return back()->withErrors($validator)->withInput(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
$category = new Category(); |
|
64
|
|
|
|
|
65
|
1 |
|
$this->saveOnDb($request, $category); |
|
66
|
|
|
|
|
67
|
1 |
|
return redirect()->route('categories.index') |
|
68
|
1 |
|
->with('success', __('laravel-smart-blog::messages.category_added_successfully')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Display the specified resource. |
|
73
|
|
|
* |
|
74
|
|
|
* @param \DavideCasiraghi\LaravelSmartBlog\Models\Category $category |
|
75
|
|
|
* @return \Illuminate\Http\Response |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function show(Category $category) |
|
78
|
|
|
{ |
|
79
|
1 |
|
return view('laravel-smart-blog::categories.show', compact('category')); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Show the form for editing the specified resource. |
|
84
|
|
|
* |
|
85
|
|
|
* @param \DavideCasiraghi\LaravelSmartBlog\Models\Category $category |
|
86
|
|
|
* @return \Illuminate\Http\Response |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function edit(Category $category) |
|
89
|
|
|
{ |
|
90
|
1 |
|
return view('laravel-smart-blog::categories.edit', compact('category')); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Update the specified resource in storage. |
|
95
|
|
|
* |
|
96
|
|
|
* @param \Illuminate\Http\Request $request |
|
97
|
|
|
* @param \DavideCasiraghi\LaravelSmartBlog\Models\Category $category |
|
98
|
|
|
* @return \Illuminate\Http\Response |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function update(Request $request, Category $category) |
|
101
|
|
|
{ |
|
102
|
2 |
|
request()->validate([ |
|
103
|
2 |
|
'name' => 'required', |
|
104
|
|
|
]); |
|
105
|
|
|
|
|
106
|
1 |
|
$this->saveOnDb($request, $category); |
|
107
|
|
|
|
|
108
|
1 |
|
return redirect()->route('categories.index') |
|
109
|
1 |
|
->with('success', __('laravel-smart-blog::messages.category_updated_successfully')); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Remove the specified resource from storage. |
|
114
|
|
|
* |
|
115
|
|
|
* @param \DavideCasiraghi\LaravelSmartBlog\Models\Category $category |
|
116
|
|
|
* @return \Illuminate\Http\Response |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function destroy(Category $category) |
|
119
|
|
|
{ |
|
120
|
1 |
|
$category->delete(); |
|
121
|
|
|
|
|
122
|
1 |
|
return redirect()->route('categories.index') |
|
123
|
1 |
|
->with('success', __('laravel-smart-blog::messages.category_deleted_successfully')); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/***************************************************************************/ |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Save/Update the record on DB. |
|
130
|
|
|
* |
|
131
|
|
|
* @param \Illuminate\Http\Request $request |
|
132
|
|
|
* @return void |
|
133
|
|
|
*/ |
|
134
|
2 |
|
public function saveOnDb($request, $category) |
|
135
|
|
|
{ |
|
136
|
2 |
|
$category->translateOrNew('en')->name = $request->get('name'); |
|
137
|
2 |
|
$category->translateOrNew('en')->description = $request->get('description'); |
|
138
|
2 |
|
$category->translateOrNew('en')->slug = Str::slug($category->name, '-'); |
|
139
|
|
|
|
|
140
|
2 |
|
$category->save(); |
|
141
|
2 |
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|