Pass URL parameter from Page to Widget

I have a page that calls a widget
<?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,
];
}
}
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
1 Reply
TheRealTeeHill
TheRealTeeHillOP5mo ago
I figured it out... In the page I have this:
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,
]),
];
}
and in the widget:
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();
}
maybe there is a better way?
Want results from more Discord servers?
Add your server