3

I have PostResource in my filament application.

Post modal schema

Posts
    - id
    - title
    - slug
    - description
    - created_by
    - status

Now, whenever I create a new post and it redirects to the Edit page and URL accessing like http://127.0.0.1"8000/posts/1/edit. but can we add a slug in the URL?

Excepted URL: http://127.0.0.1"8000/posts/<SLUG>/edit

In my PostResource the routes

public static function getPages(): array
{
    return [
        'index' => Pages\ListPosts::route('/'),
        'create' => Pages\CreatePost::route('/create'),
        // here, it binding route model, I want to change with slug
        'edit' => Pages\EditPost::route('/{record}/edit'),
    ];
}
Mitesh Rathod
  • 879
  • 5
  • 18
  • You should read basic Laravel documentation – SiZE Jun 27 '23 at 07:41
  • @SiZE, I know how we can set the slug in the route, but it's a TALL Stack Filament. and I am trying to implement in the Filament that works with the route model binding by default – Mitesh Rathod Jun 27 '23 at 08:57
  • @SiZE it's a filament question, not about core Laravel. – Khyati Bhojawala Jun 27 '23 at 09:00
  • @KhyatiBhojawala There are features in core Laravel that Filament will happily use. In this case, you want to look into `getRouteKey` and `getRouteKeyName` for your Eloquent models rather than having to manually `url()` every time. – ceejayoz Jun 27 '23 at 11:53
  • @ceejayoz , Thanks for the guidance; would you please share any example for the same? – Khyati Bhojawala Jun 28 '23 at 05:13

1 Answers1

2

It's simple, is that...

In your PostResource you need to add ->url() and as a route parameter add $record->slug.


Tables\Actions\EditAction::make()
    ->url(fn (Model $record): string => static::getUrl('edit', ['record' => $record->slug])),


Updated - 28-06-2023


If you change the slug in the clickable tr, you need to change it in your List class and override the getTableRecordUrlUsing() function.

PlanResource > Pages > ListPlans

use Closure;

class ListPlans extends ListRecords
{
    // ...
    
    protected function getTableRecordUrlUsing(): ?Closure
    {
        return function (Model $record): ?string {
            foreach (['view', 'edit'] as $action) {
                $action = $this->getCachedTableAction($action);

                if (! $action) {
                    continue;
                }

                $action->record($record);

                if ($action->isHidden()) {
                    continue;
                }

                $url = $action->getUrl();

                if (! $url) {
                    continue;
                }

                return $url;
            }

            $resource = static::getResource();

            foreach (['view', 'edit'] as $action) {
                if (! $resource::hasPage($action)) {
                    continue;
                }

                if (! $resource::{'can' . ucfirst($action)}($record)) {
                    continue;
                }
                // here you need to add slug
                return $resource::getUrl($action, ['record' => $record->slug]);
            }

            return null;
        };
    }
}
Mitesh Rathod
  • 879
  • 5
  • 18
Khyati Bhojawala
  • 146
  • 1
  • 11