如何在FilamentPHP中传递路由参数?

ds97pgxw  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(142)

我想在创建剧集时传递站id。我对EpisodeResource文件做了以下更改。

'edit' => Pages\EditEpisode::route(
                '/{station}/{record}/edit'
            ),

这样做的问题是,即使我的创建和编辑路线改变了,但我不能更新或创建操作。似乎POST请求/livewire/update不考虑station id。
我应该把它改成http query request吗?

thtygnil

thtygnil1#

我认为你是对的; Livewire不接收station参数。所以解决方法是每次参数为空时都要设置它。像这样(在你的例子中,EpisodeResource):

public static function getUrl(string $name = 'index', array $parameters = [], bool $isAbsolute = true, ?string $panel = null, ?Model $tenant = null): string
{
    if (!isset($parameters['station'])) {
        $parameters['station'] = self::getCurrentStation(); //Route::current()->parameter('station')
    }

    return parent::getUrl($name, $parameters, $isAbsolute, $panel, $tenant);
}

private static function getCurrentStation()
{
    $station = request('station');
    if(isset($station)){
       return $station;
    }
    
    //try to get the Model from the previous known route
    $previousRoute = Route::getRoutes()->match(request()->create(url()->previousPath()));
    if (isset($previousRoute)) {
        //the model is not in the request (this is probably a Livewire request)
        //reset it
        $station = $previousRoute->parameter('station');
        request()->merge(['station' => $station]);

        return $station;
    }
    
    return null;
}

也请看这个问题:https://github.com/filamentphp/filament/issues/7104和这个讨论:https://github.com/filamentphp/filament/discussions/6099

相关问题