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
JordyOP3w ago
No description
Jordy
JordyOP3w ago
any decent way to get around this? dont really want to add it to the session just to pull it..
Jordy
JordyOP3w 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
JordyOP3w ago
can throw this in a PR if it indeed doesnt break anything..
LeandroFerreira
use \Filament\Resources\Pages\Concerns\InteractsWithRecord;
...

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

$this->getRecord()
Jordy
JordyOP3w ago
this would throw $record must not be accessed before initialization if used in getData()
LeandroFerreira
try protected static bool $isLazy = false;
Jordy
JordyOP3w ago
same issue :/ assuming because its a widget, not a page
LeandroFerreira
where are you rendering the chart?
Jordy
JordyOP3w 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
is it a resource custom page?
Jordy
JordyOP3w 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 Harrin3w 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
JordyOP3w ago
interesting.. didnt know you could do that, it does throw but its trying :kekw:
No description
Dan Harrin
Dan Harrin3w ago
instead of ::make(), try returning the record in an array from getWidgetData() on the page
Jordy
JordyOP3w 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 Harrin3w ago
where are you putting that method on the widget or the page
Jordy
JordyOP3w ago
the page
Dan Harrin
Dan Harrin3w ago
wdym by calling the widget in the view
Jordy
JordyOP3w ago
like this
Solution
Dan Harrin
Dan Harrin3w ago
whats wrong with @livewire(GrainChart::class, ['record' => $this->record])
Jordy
JordyOP3w ago
it would throw $record must not be accessed before initialization wtf it works now smh :kekw:
Dan Harrin
Dan Harrin3w 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
JordyOP3w 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?