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..
24 Replies
any decent way to get around this? dont really want to add it to the session just to pull it..
I dont see why the mount is set by filament tbh, edited
ChartWidget
to run said logic in construct and nothing breaks for mecan throw this in a PR if it indeed doesnt break anything..
this would throw
$record must not be accessed before initialization
if used in getData()
try
protected static bool $isLazy = false;
same issue :/
assuming because its a widget, not a page
where are you rendering the chart?
inside the view of a page
have another issue which is getting the chart in the pdf I want to export but thats a different issue kek
is it a resource custom page?
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 🤦♂️
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 anythinginteresting.. didnt know you could do that, it does throw but its trying :kekw:
instead of ::make(), try returning the record in an array from getWidgetData() on the page
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
where are you putting that method
on the widget or the page
the page
wdym by calling the widget in the view
like this
Solution
whats wrong with
@livewire(GrainChart::class, ['record' => $this->record])
it would throw $record must not be accessed before initialization
wtf it works now
smh :kekw:
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 propertycheers.. now this works.. I need to find a way to turn the chart into an image or somehow slap it in a pdf