Using a custom component in a Form
Hi - I think I've overlooked something here. I am creating a livewire-component to show an overview, which is then polled every 10 seconds. So I made a new component via
php artisan make:livewire mycomponent
and now I wanna use that within getFormSchema()
inside a section, but it does not have the usual ::make()
function, which all FilamentComponents have. Do I have to define a make
-function myself or just use some Trait?24 Replies
You can wrap your Livewire component in a Filament form component. It's not ideal but easy to setup.
I think you could even use
Component::make()->view( ... )
directly, if you don't need to encapsulate anything else in the form component.just use a ViewField and set
->view('my-component.blade.php')
and put <livewire:my-livewire-component/>
in that blade file.But the ViewField requires a $name parameter, no?
Sorry, wrong class, you can use the View layout component.
View::make('filament.forms.components.wizard')
if you don't need schema you don't have to provide it. I don't think.Perfect - Thanks guys! 🙂
I tried your approach, but it ends in the Page not beeing loaded (loading keeps on scrolling there...)
view:
Component.php:
And I implemented it like this:
The wire poll needs to be inside your livewire components view.
Not your view layout blade file.
Okay - now I am confused...
For the page-loading - the render()-function was the problem there...
Basically - what I want is a reuseable Livewire-Component...
Where the polling does poll the component and not the full page...
Understandable, it’s hard to explain. In your form View::make() points to a blade file. That blade file has nothing in it but the <livewire> directive. That directive loads your livewire component’s blade file that has all you live wire stuff in it. So it’s two Separate files.
Ahhhhhh - okay, I think I get what are you are saying! One second! 😁
Basically you can’t load a livewire component directly in a form.
Okay. I moved the components view-file so it looks like this:
But now it tells me
Undefined variable $timestamp
... o.O
shouldn't the mount-function define it?wait. what are you trying to do exactly? is the timestamp a field on the record?
Nope - right now it's just a variable for me to test that polling works. 🙂
Okay - one step back. Right now it looks like this:
Form-Page:
components.my-components.sysinternals.container-full-status looks like this:
the ContainerFullStatus.php like this:
And the livewire.container-full-status has this:
try
ah - the classic typo! thanks! 🙂
After that it works, but polling does not.
Modified the component-view to
which (according to the livewire-docs) should change timestamp every 2 seconds, but it keeps beeing static. already tried the obligatory filament:upgrade...
polling-docs: https://laravel-livewire.com/docs/2.x/polling
Livewire
Polling | Livewire
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
mount only runs once when the component is initialized. you'll need to make a method to run on the interval that updates the $timeStamp property
wire:poll="updateTimeStamp"
something like that
okay... kinda weird... I tried it in a view directly before that and it updated the timestamp... huh... the more you know... thank you very much! 🙂
yep - that works. Kinda weird behavior tho that it needs a function, when the docs don't explicitly require one... 🙂
it's probably mentioned somewhere else in the docs. 🙂
I thought so aswell... But before that I tried this:
with components.my-components.sysinternals.container-full-status looking like this:
And that worked fine - so there was no need to define any function.
But like I said - i wanted to poll only the component and not the whole page, so I guess that there is something going on in the background.
yea, the poll on the page in Filament is changing the property, but since you took it out of the "page" context, it needs it's own method for polling
ah okay! but I got that right, so that with the custom component and the custom function it only polls the component and not the whole page?
correct
thank you very much good sir! 🙂