1
|
|
|
<?php namespace Arcanesoft\Blog\Http\Controllers\Admin; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Blog\Http\Requests\Admin\Categories\CreateCategoryRequest; |
4
|
|
|
use Arcanesoft\Blog\Http\Requests\Admin\Categories\UpdateCategoryRequest; |
5
|
|
|
use Arcanesoft\Blog\Models\Category; |
6
|
|
|
use Arcanesoft\Blog\Policies\CategoriesPolicy; |
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CategoriesController |
11
|
|
|
* |
12
|
|
|
* @package Arcanesoft\Blog\Http\Controllers\Admin |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class CategoriesController extends Controller |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The category model. |
24
|
|
|
* |
25
|
|
|
* @var \Arcanesoft\Blog\Models\Category |
26
|
|
|
*/ |
27
|
|
|
private $category; |
28
|
|
|
|
29
|
|
|
/* ----------------------------------------------------------------- |
30
|
|
|
| Constructor |
31
|
|
|
| ----------------------------------------------------------------- |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* CategoriesController constructor. |
36
|
|
|
* |
37
|
|
|
* @param \Arcanesoft\Blog\Models\Category $category |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Category $category) |
40
|
|
|
{ |
41
|
|
|
parent::__construct(); |
42
|
|
|
|
43
|
|
|
$this->category = $category; |
44
|
|
|
|
45
|
|
|
$this->setCurrentPage('blog-categories'); |
46
|
|
|
$this->addBreadcrumbRoute(trans('blog::categories.titles.categories'), 'admin::blog.categories.index'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* ----------------------------------------------------------------- |
50
|
|
|
| Main Methods |
51
|
|
|
| ----------------------------------------------------------------- |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
public function index($trashed = false) |
55
|
|
|
{ |
56
|
|
|
$this->authorize(CategoriesPolicy::PERMISSION_LIST); |
57
|
|
|
|
58
|
|
|
$categories = $this->category->with(['posts'])->when($trashed, function ($query) { |
|
|
|
|
59
|
|
|
return $query->onlyTrashed(); |
60
|
|
|
})->paginate(30); |
61
|
|
|
|
62
|
|
|
$this->setTitle($title = trans('blog::categories.titles.categories-list')); |
63
|
|
|
$this->addBreadcrumb($title); |
64
|
|
|
|
65
|
|
|
return $this->view('admin.categories.index', compact('categories', 'trashed')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function trash() |
69
|
|
|
{ |
70
|
|
|
return $this->index(true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function create() |
74
|
|
|
{ |
75
|
|
|
$this->authorize(CategoriesPolicy::PERMISSION_CREATE); |
76
|
|
|
|
77
|
|
|
$this->setTitle($title = trans('blog::categories.titles.create-category')); |
78
|
|
|
$this->addBreadcrumb($title); |
79
|
|
|
|
80
|
|
|
return $this->view('admin.categories.create'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function store(CreateCategoryRequest $request) |
84
|
|
|
{ |
85
|
|
|
$this->authorize(CategoriesPolicy::PERMISSION_CREATE); |
86
|
|
|
|
87
|
|
|
$category = Category::createOne($request->getValidatedData()); |
88
|
|
|
|
89
|
|
|
$this->transNotification('created', ['name' => $category->name], $category->toArray()); |
90
|
|
|
|
91
|
|
|
return redirect()->route('admin::blog.categories.index'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function show(Category $category) |
95
|
|
|
{ |
96
|
|
|
$this->authorize(CategoriesPolicy::PERMISSION_SHOW); |
97
|
|
|
|
98
|
|
|
$category->load(['posts']); |
99
|
|
|
|
100
|
|
|
$this->setTitle($title = trans('blog::categories.titles.category-details')); |
101
|
|
|
$this->addBreadcrumb($category->name); |
102
|
|
|
|
103
|
|
|
return $this->view('admin.categories.show', compact('category')); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function edit(Category $category) |
107
|
|
|
{ |
108
|
|
|
$this->authorize(CategoriesPolicy::PERMISSION_UPDATE); |
109
|
|
|
|
110
|
|
|
$this->setTitle($title = trans('blog::categories.titles.edit-category')); |
111
|
|
|
$this->addBreadcrumb($title); |
112
|
|
|
|
113
|
|
|
return $this->view('admin.categories.edit', compact('category')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function update(Category $category, UpdateCategoryRequest $request) |
117
|
|
|
{ |
118
|
|
|
$this->authorize(CategoriesPolicy::PERMISSION_UPDATE); |
119
|
|
|
|
120
|
|
|
$category->updateOne($request->getValidatedData()); |
121
|
|
|
|
122
|
|
|
$this->transNotification('updated', ['name' => $category->name], $category->toArray()); |
123
|
|
|
|
124
|
|
|
return redirect()->route('admin::blog.categories.show', [$category]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function delete(Category $category) |
128
|
|
|
{ |
129
|
|
|
$this->authorize(CategoriesPolicy::PERMISSION_DELETE); |
130
|
|
|
|
131
|
|
|
try { |
132
|
|
|
$category->trashed() ? $category->forceDelete() : $category->delete(); |
133
|
|
|
|
134
|
|
|
return $this->jsonResponseSuccess([ |
135
|
|
|
'message' => $this->transNotification('deleted', ['name' => $category->name], $category->toArray()) |
136
|
|
|
]); |
137
|
|
|
} |
138
|
|
|
catch(\Exception $e) { |
139
|
|
|
return $this->jsonResponseError($e->getMessage(), 500); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function restore(Category $category) |
144
|
|
|
{ |
145
|
|
|
$this->authorize(CategoriesPolicy::PERMISSION_UPDATE); |
146
|
|
|
|
147
|
|
|
try { |
148
|
|
|
$category->restore(); |
149
|
|
|
|
150
|
|
|
return $this->jsonResponseSuccess([ |
151
|
|
|
'message' => $this->transNotification('restored', ['name' => $category->name], $category->toArray()) |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
catch (\Exception $e) { |
155
|
|
|
return $this->jsonResponseError($e->getMessage(), 500); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/* ----------------------------------------------------------------- |
160
|
|
|
| Other Methods |
161
|
|
|
| ----------------------------------------------------------------- |
162
|
|
|
*/ |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Notify with translation. |
166
|
|
|
* |
167
|
|
|
* @todo: Refactor this methods to the core package ? |
168
|
|
|
* |
169
|
|
|
* @param string $action |
170
|
|
|
* @param array $replace |
171
|
|
|
* @param array $context |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
protected function transNotification($action, array $replace = [], array $context = []) |
176
|
|
|
{ |
177
|
|
|
$title = trans("blog::categories.messages.{$action}.title"); |
178
|
|
|
$message = trans("blog::categories.messages.{$action}.message", $replace); |
179
|
|
|
|
180
|
|
|
Log::info($message, $context); |
181
|
|
|
$this->notifySuccess($message, $title); |
182
|
|
|
|
183
|
|
|
return $message; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: