Pass URL parameter from Page to Widget
I have a page that calls a widget
I want to pass a variable via the URL to the widget so I can filter the widget data - http://mysite.com/admin/vessel-finder?fleet=north
How can I collect "fleet" in the VesselLocationGoogleMapWidget?
Cheers
<?php
namespace App\Filament\Pages;
use App\Filament\Resources\VesselLocationResource\Widgets\VesselLocationGoogleMapWidget;
use Filament\Pages\Page;
use Illuminate\Http\Request;
class VesselFinder extends Page
{
public $fleet;
protected static ?string $navigationIcon = 'icon-map';
protected static ?string $navigationGroup = 'Maritime Overview';
protected static ?int $navigationSort = 5;
protected static string $view = 'filament.pages.vessel-finder';
public function mount(Request $request)
{
$this->fleet = $request->query('fleet');
}
protected function getHeaderWidgets(): array
{
return [
VesselLocationGoogleMapWidget::class,
];
}
protected function getHeaderWidgetsData(): array
{
return [
'fleet' => $this->fleet,
];
}
}
<?php
namespace App\Filament\Pages;
use App\Filament\Resources\VesselLocationResource\Widgets\VesselLocationGoogleMapWidget;
use Filament\Pages\Page;
use Illuminate\Http\Request;
class VesselFinder extends Page
{
public $fleet;
protected static ?string $navigationIcon = 'icon-map';
protected static ?string $navigationGroup = 'Maritime Overview';
protected static ?int $navigationSort = 5;
protected static string $view = 'filament.pages.vessel-finder';
public function mount(Request $request)
{
$this->fleet = $request->query('fleet');
}
protected function getHeaderWidgets(): array
{
return [
VesselLocationGoogleMapWidget::class,
];
}
protected function getHeaderWidgetsData(): array
{
return [
'fleet' => $this->fleet,
];
}
}
1 Reply
I figured it out...
In the page I have this:
and in the widget:
maybe there is a better way?
public function mount(Request $request)
{
$this->fleet = $request->query('fleet', null);
}
protected function getHeaderWidgets(): array
{
return [
VesselLocationGoogleMapWidget::make([
'fleet' => $this->fleet,
]),
];
}
public function mount(Request $request)
{
$this->fleet = $request->query('fleet', null);
}
protected function getHeaderWidgets(): array
{
return [
VesselLocationGoogleMapWidget::make([
'fleet' => $this->fleet,
]),
];
}
public ?string $fleet = null;
if ($this->fleet) {
$locations = VesselLocation::latestLocations()
->whereHas('vessel.fleet', function ($query) {
$query->where('name', $this->fleet);
})
->with('vessel', 'vessel.fleet')
->get();
} else {
$locations = VesselLocation::latestLocations()->with('vessel', 'vessel.fleet')->get();
}
public ?string $fleet = null;
if ($this->fleet) {
$locations = VesselLocation::latestLocations()
->whereHas('vessel.fleet', function ($query) {
$query->where('name', $this->fleet);
})
->with('vessel', 'vessel.fleet')
->get();
} else {
$locations = VesselLocation::latestLocations()->with('vessel', 'vessel.fleet')->get();
}