F
Filament3mo ago
Jordy

Passing a record to a chart

I want to pass a record to a ChartWidget, to base the data of of that record. However I cannot add a parameter to the mount method because it must be compatible to the parent ChartWidget class that doesnt accept any params..
Solution:
whats wrong with @livewire(GrainChart::class, ['record' => $this->record])
Jump to solution
24 Replies
Jordy
JordyOP3mo ago
No description
Jordy
JordyOP3mo ago
any decent way to get around this? dont really want to add it to the session just to pull it..
Jordy
JordyOP3mo ago
I dont see why the mount is set by filament tbh, edited ChartWidget to run said logic in construct and nothing breaks for me
No description
Jordy
JordyOP3mo ago
can throw this in a PR if it indeed doesnt break anything..
LeandroFerreira
LeandroFerreira3mo ago
use \Filament\Resources\Pages\Concerns\InteractsWithRecord;
...

$this->getRecord()
use \Filament\Resources\Pages\Concerns\InteractsWithRecord;
...

$this->getRecord()
Jordy
JordyOP3mo ago
this would throw $record must not be accessed before initialization if used in getData()
LeandroFerreira
LeandroFerreira3mo ago
try protected static bool $isLazy = false;
Jordy
JordyOP3mo ago
same issue :/ assuming because its a widget, not a page
LeandroFerreira
LeandroFerreira3mo ago
where are you rendering the chart?
Jordy
JordyOP3mo ago
inside the view of a page
<@php use App\Filament\Resources\Management\ProductResource\Widgets\GrainChart; @endphp
<x-filament-panels::page>
<x-filament::section>
<x-pdf.product-information-sheet :product="$this->record"/>
</x-filament::section>
@livewire(GrainChart::class)
//Would like to pass the record like:
//@livewire(GrainChart::class, ['record' => $this->record]);
</x-filament-panels::page>
<@php use App\Filament\Resources\Management\ProductResource\Widgets\GrainChart; @endphp
<x-filament-panels::page>
<x-filament::section>
<x-pdf.product-information-sheet :product="$this->record"/>
</x-filament::section>
@livewire(GrainChart::class)
//Would like to pass the record like:
//@livewire(GrainChart::class, ['record' => $this->record]);
</x-filament-panels::page>
have another issue which is getting the chart in the pdf I want to export but thats a different issue kek
LeandroFerreira
LeandroFerreira3mo ago
is it a resource custom page?
Jordy
JordyOP3mo ago
yes my fix doesnt work either since getData() seems the be ran before mount() ...my coffee just kicked in, the page was already using route model binding.. so why not just pull it from the route 🤦‍♂️
protected function getData(): array
{
$record = request()->route('record');

....
}
protected function getData(): array
{
$record = request()->route('record');

....
}
Dan Harrin
Dan Harrin3mo ago
public Model $record on the chart widget, then ChartWidget::make(['record' => $this->getRecord()]) on the page you dont need to add a constructor or mount or anything
Jordy
JordyOP3mo ago
interesting.. didnt know you could do that, it does throw but its trying :kekw:
No description
Dan Harrin
Dan Harrin3mo ago
instead of ::make(), try returning the record in an array from getWidgetData() on the page
Jordy
JordyOP3mo ago
public function getWidgetData(): array
{
return [
'record' => $this->record,
];
}
public function getWidgetData(): array
{
return [
'record' => $this->record,
];
}
throws $record must not be accessed before initialization (I try to dd it in getData) worth nothing I only call the widget in the view.. havent registered it on the actual page
Dan Harrin
Dan Harrin3mo ago
where are you putting that method on the widget or the page
Jordy
JordyOP3mo ago
the page
Dan Harrin
Dan Harrin3mo ago
wdym by calling the widget in the view
Jordy
JordyOP3mo ago
like this
Solution
Dan Harrin
Dan Harrin3mo ago
whats wrong with @livewire(GrainChart::class, ['record' => $this->record])
Jordy
JordyOP3mo ago
it would throw $record must not be accessed before initialization wtf it works now smh :kekw:
Dan Harrin
Dan Harrin3mo ago
if you are rendering widgets yourself you need to pass the data as we would have done when we render we do @livewire(GrainChart::class, $this->getWidgetData()) and then you can just treat it like a normal livewire component, if you pass a model into a livewire component it can be automatically assigned to a public property
Jordy
JordyOP3mo ago
cheers.. now this works.. I need to find a way to turn the chart into an image or somehow slap it in a pdf

Did you find this page helpful?