diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 25969e6a..b6aeca16 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -312,7 +312,7 @@ public function hydrate(Closure $callback): static public function resolveHydrate(Request $request, Model $model, mixed $value): void { if (is_null($this->hydrateResolver)) { - $this->hydrateResolver = function () use ($model, $value): void { + $this->hydrateResolver = function (Request $request, Model $model, $value): void { $model->setAttribute($this->getModelAttribute(), $value); }; } diff --git a/src/Fields/Slug.php b/src/Fields/Slug.php index 49966bf1..de7fcf81 100644 --- a/src/Fields/Slug.php +++ b/src/Fields/Slug.php @@ -31,6 +31,11 @@ class Slug extends Text */ protected bool $unique = false; + /** + * Indicates if the slug field is nullable. + */ + protected bool $nullable = false; + /** * The slug resolver. */ @@ -48,15 +53,23 @@ public function __construct(string $label, string $modelAttribute = null) $this->unique(); } + /** + * Set the "nullable" property. + */ + public function nullable(bool $value = true): static + { + $this->nullable = $value; + + return $this; + } + /** * {@inheritdoc} */ public function persist(Request $request, Model $model, mixed $value): void { - if ($model->exists || ! empty($value)) { - parent::persist($request, $model, $value); - } else { - $model->saved(function (Model $model) use ($request, $value): void { + if (! $model->exists) { + $model->saved(function (Model $model) use ($request): void { $value = $this->generate($request, $model); $this->resolveHydrate($request, $model, $value); @@ -66,6 +79,8 @@ public function persist(Request $request, Model $model, mixed $value): void }); }); } + + parent::persist($request, $model, $value); } /** @@ -73,7 +88,13 @@ public function persist(Request $request, Model $model, mixed $value): void */ public function getValueForHydrate(Request $request): mixed { - return Str::slug(parent::getValueForHydrate($request), $this->separator); + $value = parent::getValueForHydrate($request); + + if (! $this->nullable && empty($value)) { + $value = Str::random(); + } + + return Str::slug($value, $this->separator); } /** diff --git a/src/Http/Controllers/ResourceController.php b/src/Http/Controllers/ResourceController.php index 1efc5ab0..cd7e460e 100644 --- a/src/Http/Controllers/ResourceController.php +++ b/src/Http/Controllers/ResourceController.php @@ -3,6 +3,7 @@ namespace Cone\Root\Http\Controllers; use Cone\Root\Http\Middleware\AuthorizeResource; +use Cone\Root\Resources\Resource; use Cone\Root\Support\Alert; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; @@ -25,10 +26,8 @@ public function __construct() /** * Display a listing of the resource. */ - public function index(Request $request): Response + public function index(Request $request, Resource $resource): Response { - $resource = $request->route('resource'); - if ($resource->getPolicy()) { $this->authorize('viewAny', $resource->getModel()); } @@ -42,10 +41,8 @@ public function index(Request $request): Response /** * Show the form for creating a new resource. */ - public function create(Request $request): Response + public function create(Request $request, Resource $resource): Response { - $resource = $request->route('resource'); - if ($resource->getPolicy()) { $this->authorize('create', $resource->getModel()); } @@ -59,10 +56,8 @@ public function create(Request $request): Response /** * Store a newly created resource in storage. */ - public function store(Request $request): RedirectResponse + public function store(Request $request, Resource $resource): RedirectResponse { - $resource = $request->route('resource'); - if ($resource->getPolicy()) { $this->authorize('create', $resource->getModel()); } @@ -78,10 +73,8 @@ public function store(Request $request): RedirectResponse /** * Show the form for editing the specified resource. */ - public function edit(Request $request, Model $model): Response + public function edit(Request $request, Resource $resource, Model $model): Response { - $resource = $request->route('resource'); - if ($resource->getPolicy()) { $this->authorize('update', $model); } @@ -95,10 +88,8 @@ public function edit(Request $request, Model $model): Response /** * Update the specified resource in storage. */ - public function update(Request $request, Model $model): RedirectResponse + public function update(Request $request, Resource $resource, Model $model): RedirectResponse { - $resource = $request->route('resource'); - if ($resource->getPolicy()) { $this->authorize('update', $model); } @@ -112,10 +103,8 @@ public function update(Request $request, Model $model): RedirectResponse /** * Remove the specified resource from storage. */ - public function destroy(Request $request, Model $model): RedirectResponse + public function destroy(Request $request, Resource $resource, Model $model): RedirectResponse { - $resource = $request->route('resource'); - $trashed = in_array(SoftDeletes::class, class_uses_recursive($model)) && $model->trashed(); if ($resource->getPolicy()) { @@ -131,10 +120,8 @@ public function destroy(Request $request, Model $model): RedirectResponse /** * Restore the specified resource in storage. */ - public function restore(Request $request, Model $model): RedirectResponse + public function restore(Request $request, Resource $resource, Model $model): RedirectResponse { - $resource = $request->route('resource'); - if ($resource->getPolicy()) { $this->authorize('restore', $model); } diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index c71c78f5..7b3bd726 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -22,6 +22,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Http\Request; +use Illuminate\Routing\Events\RouteMatched; use Illuminate\Routing\Router; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Gate; @@ -284,6 +285,14 @@ public function registerRoutes(Request $request, Router $router): void }); } + /** + * Handle the route matched event. + */ + public function routeMatched(RouteMatched $event): void + { + // + } + /** * Get the instance as an array. */ diff --git a/src/RootServiceProvider.php b/src/RootServiceProvider.php index 8de09cf6..dee0143c 100644 --- a/src/RootServiceProvider.php +++ b/src/RootServiceProvider.php @@ -117,6 +117,10 @@ protected function registerRoutes(): void $root = $this->app->make(Root::class); + $this->app['router']->bind('resource', function (string $key) use ($root): Resource { + return $root->resources->resolve($key); + }); + $this->app['router']->bind('resourceModel', function (string $id, Route $route): Model { return $route->parameter('resource')->resolveRouteBinding($this->app['request'], $id); });