Use Table-Widget in Livewire Component / Outside Admin Panel
I'm using a table widget to display data inside the dashboard. This data should also be displayed in the public frontend (outside the admin panel). What's the best way to display this table widget there without duplicating the whole class? Everything is exactly the same.
Thank you!
19 Replies
Not sure how others do it but I think adding static methods to you resource class can be a simple solution :
Sorry you're using a widget but the idea still applies
so you mean calling the static class inside the livewire component as well? I think it expects an array
or I didn't quite understand the approach yet
can you explain it more? Currently I have a widget class and my livewire component
Yeah, the idea is to define your columns in one place (e.g. a static method) and call it for each table, instead of duplicating the config
Hope that makes sense...
okay, but there's no way to get the entire widget rendered outside the admin panel? Background: Even the defined actions and query should be the same.
Thank you already!
Good point, haha! As far as I know, a widget is basically a simple livewire component... you may be able to just render it in a front-end view with
@livewire()
in the source code of filament I found the following table-widget.blade.php view:
But I don't how to get $this->table or what to replace it with.
Not sure you need to change that... how are you adding the component on your front-end page?
my idea was to create a new livewire component and then somehow implement the widget there so $this->table would be available. But I never done anything like this in filament before
The widget is already a livewire component. To use it in your front-end, I think you just need to register it manually in your AppServiceProvider, something like this:
Then
@livewire('my-custom-widget')
in your viewhm... okay I registered it, but if it's being used with
@livewire()
the server crashes with empty response 😅
But if I do something wrong inside the widget a exception gets thrown, so it does get through somehow...I just tested it here, I'm able to use a table widget outside of the admin panel. Not sure where your error comes from. Check
storage/logs/laravel.log
ah now it works, just had the wrong class.
Thank you!!
Nice!
Okay to save some more duplicate code I decided to implement <x-filament::layouts.base> to my view so I don't have to create a new layout. It works fine but there's just one little thing that's missing: I implemented a custom theme for the admin panel (tailwind blue colors) and that's not recognized inside the base layout. Do you know how I can set this to the correct theme? Would be awesome!
Can you verify that your custom admin css is loaded on the front-end?
In the filament base layout component the css loads with this logic:
In the stylesheet in the HTML is this: http://localhost/filament/assets/app.css
Inside the admin panel it's this: http://localhost/build/assets/filament.css
so I don't know how filament handles the custom path internally, but it doesn't work for the frontend at the moment
Yeah... I don't think the core layouts are designed to be used outside of the admin but I feel like you're close to a working solution. Maybe this can be done in your Vite config?
I debugged through the code and it seems like that registerViteTheme doesn't get called, so it loads the default theme. By any chance, you do know why this is not being called outside the admin panel? It's registered in my AppServiceProvider as the docs said.
ah, now I got it. I removed
Filament::serving(function () {}
so it always gets called.Cool!