diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php
index 16b76fd..b9447b9 100644
--- a/app/Http/Controllers/CategoryController.php
+++ b/app/Http/Controllers/CategoryController.php
@@ -5,6 +5,8 @@
use App\Http\Requests\CategoryRequest;
use App\Models\Category;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
+use Illuminate\Support\Facades\Redirect;
+use Illuminate\Support\Facades\View;
class CategoryController extends Controller
{
@@ -14,14 +16,24 @@ public function index()
{
$this->authorize('viewAny', Category::class);
- return view('categories.index')->with(['categories' => Category::all()]);
+ return View::make('categories.index',
+ ['categories' => Category::all()]);
+ }
+
+ public function create()
+ {
+ $this->authorize('create', Category::class);
+
+ return View::make('categories.create');
}
public function store(CategoryRequest $request)
{
$this->authorize('create', Category::class);
- return Category::create($request->validated());
+ Category::create($request->validated());
+
+ return Redirect::route('categories.index');
}
public function show(Category $category)
diff --git a/app/Http/Requests/CategoryRequest.php b/app/Http/Requests/CategoryRequest.php
index d78bd68..9f35641 100644
--- a/app/Http/Requests/CategoryRequest.php
+++ b/app/Http/Requests/CategoryRequest.php
@@ -2,6 +2,7 @@
namespace App\Http\Requests;
+use App\Models\Category;
use Illuminate\Foundation\Http\FormRequest;
class CategoryRequest extends FormRequest
@@ -15,6 +16,12 @@ public function rules(): array
public function authorize(): bool
{
- return auth()->check();
+ if ($this->routeIs('categories.store')) {
+ return $this->user()->can('create', Category::class);
+ } elseif ($this->routeIs('categories.update')) {
+ return $this->user()->can('update', $this->route('category'));
+ }
+
+ return false;
}
}
diff --git a/app/Policies/CategoryPolicy.php b/app/Policies/CategoryPolicy.php
index feeb5ef..4ff52c9 100644
--- a/app/Policies/CategoryPolicy.php
+++ b/app/Policies/CategoryPolicy.php
@@ -5,6 +5,7 @@
use App\Models\Category;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
+use Illuminate\Support\Facades\Auth;
class CategoryPolicy
{
@@ -22,26 +23,26 @@ public function view(User $user, Category $category): bool
public function create(User $user): bool
{
- return auth()->check();
+ return Auth::check();
}
public function update(User $user, Category $category): bool
{
- return auth()->check();
+ return Auth::check();
}
public function delete(User $user, Category $category): bool
{
- return auth()->check();
+ return Auth::check();
}
public function restore(User $user, Category $category): bool
{
- return auth()->check();
+ return Auth::check();
}
public function forceDelete(User $user, Category $category): bool
{
- return auth()->check();
+ return Auth::check();
}
}
diff --git a/resources/views/categories/create.blade.php b/resources/views/categories/create.blade.php
new file mode 100644
index 0000000..2439dda
--- /dev/null
+++ b/resources/views/categories/create.blade.php
@@ -0,0 +1,15 @@
+
+ New Category
+
+
+
diff --git a/resources/views/categories/index.blade.php b/resources/views/categories/index.blade.php
index 94a9c2f..ed41f0f 100644
--- a/resources/views/categories/index.blade.php
+++ b/resources/views/categories/index.blade.php
@@ -2,16 +2,20 @@
Categories
-
+
Category |
Actions |
-
+
@foreach ($categories as $category)
-
+
{{ $category->name }} |
|
@@ -19,4 +23,6 @@
+
+ New