categories.create

This commit is contained in:
punkfairie 2025-02-21 11:35:53 -08:00
parent 9e3cb21848
commit e5837c0537
Signed by: punkfairie
GPG key ID: B3C5488E9A1A7CA6
5 changed files with 52 additions and 11 deletions

View file

@ -5,6 +5,8 @@
use App\Http\Requests\CategoryRequest; use App\Http\Requests\CategoryRequest;
use App\Models\Category; use App\Models\Category;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
class CategoryController extends Controller class CategoryController extends Controller
{ {
@ -14,14 +16,24 @@ public function index()
{ {
$this->authorize('viewAny', Category::class); $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) public function store(CategoryRequest $request)
{ {
$this->authorize('create', Category::class); $this->authorize('create', Category::class);
return Category::create($request->validated()); Category::create($request->validated());
return Redirect::route('categories.index');
} }
public function show(Category $category) public function show(Category $category)

View file

@ -2,6 +2,7 @@
namespace App\Http\Requests; namespace App\Http\Requests;
use App\Models\Category;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
class CategoryRequest extends FormRequest class CategoryRequest extends FormRequest
@ -15,6 +16,12 @@ public function rules(): array
public function authorize(): bool 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;
} }
} }

View file

@ -5,6 +5,7 @@
use App\Models\Category; use App\Models\Category;
use App\Models\User; use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization; use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Support\Facades\Auth;
class CategoryPolicy class CategoryPolicy
{ {
@ -22,26 +23,26 @@ public function view(User $user, Category $category): bool
public function create(User $user): bool public function create(User $user): bool
{ {
return auth()->check(); return Auth::check();
} }
public function update(User $user, Category $category): bool public function update(User $user, Category $category): bool
{ {
return auth()->check(); return Auth::check();
} }
public function delete(User $user, Category $category): bool public function delete(User $user, Category $category): bool
{ {
return auth()->check(); return Auth::check();
} }
public function restore(User $user, Category $category): bool public function restore(User $user, Category $category): bool
{ {
return auth()->check(); return Auth::check();
} }
public function forceDelete(User $user, Category $category): bool public function forceDelete(User $user, Category $category): bool
{ {
return auth()->check(); return Auth::check();
} }
} }

View file

@ -0,0 +1,15 @@
<x-app-layout>
<x-slot:title>New Category</x-slot>
<form action="{{ route('categories.store') }}" method="POST">
@csrf
<div class="field-row">
<label for="name">Category Name</label>
<input type="text" id="name" name="name" />
<x-input-error :messages="$errors->get('name')" />
</div>
<button type="submit">Submit</button>
</form>
</x-app-layout>

View file

@ -2,16 +2,20 @@
<x-slot:title>Categories</x-slot> <x-slot:title>Categories</x-slot>
<div class="sunken-panel"> <div class="sunken-panel">
<table> <table class="interactive">
<thead> <thead>
<tr> <tr>
<th>Category</th> <th>Category</th>
<th>Actions</th> <th>Actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody x-data="{ hovered: null }" x-on:mouseleave="hovered = null">
@foreach ($categories as $category) @foreach ($categories as $category)
<tr> <tr
id="{{ $category->id }}"
x-on:mouseenter="hovered = $event.target.id"
x-bind:class="{ 'highlighted': hovered === $el.id }"
>
<td>{{ $category->name }}</td> <td>{{ $category->name }}</td>
<td></td> <td></td>
</tr> </tr>
@ -19,4 +23,6 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<a href="{{ route('categories.create') }}">New</a>
</x-app-layout> </x-app-layout>