F
Filament13mo ago
LazyBoy

Change AdminPanelProvider primary color through model

I am trying to fetch a color value from a model and use it as a primary color in the AdminPanelProvider, but I ran into issues where I get the error "Call to a member function connection() on null" so I figured it is caused because of the booting order so I tried to fetch the value by adding
private $brandColor;

public function register(): void
{
parent::register();

$this->app->booted(function () {
$this->brandColor = Company::first()->brand_color ?? '#FF6921';
});
}
private $brandColor;

public function register(): void
{
parent::register();

$this->app->booted(function () {
$this->brandColor = Company::first()->brand_color ?? '#FF6921';
});
}
But when I access $this->brandColor outside the method it returns null, otherwise inside it returns the correct value. Is this the correct approach to change the primary color through a model and why am I facing this issue, or if I should take another approach if that is possible to do?
Solution:
``` FilamentColor::register( [ 'primary' => Color::hex('#ff0099'), ]...
Jump to solution
21 Replies
Lara Zeus
Lara Zeus13mo ago
there is a function configureUsing not sure if it documented maybe you can use it to fetch the color and set it as primary
LazyBoy
LazyBoyOP13mo ago
I tried with the following by checking available examples for configureUsing online and added configureUsing in the boot method of the AdminPanelProvider like this but had no success, not sure if im doing something wrong
public function boot()
{
Panel::configureUsing(function (Panel $panel){
$panel->colors([
'primary' => Company::first()->brand_color ?? '#FF6921'
]);
});
}
public function boot()
{
Panel::configureUsing(function (Panel $panel){
$panel->colors([
'primary' => Company::first()->brand_color ?? '#FF6921'
]);
});
}
Lara Zeus
Lara Zeus13mo ago
you can chane it to the $panel
->configureUsing(function (Panel $panel){
dd(Company::first()->brand_color);
$panel->colors([
'primary' => Company::first()->brand_color ?? '#FF6921'
]);
})
->configureUsing(function (Panel $panel){
dd(Company::first()->brand_color);
$panel->colors([
'primary' => Company::first()->brand_color ?? '#FF6921'
]);
})
see if the dd works
LazyBoy
LazyBoyOP13mo ago
It is throwing an error and returning null
Lara Zeus
Lara Zeus13mo ago
what is the error?
LazyBoy
LazyBoyOP13mo ago
App\Providers\Filament\AdminPanelProvider::panel(): Return value must be of type Filament\Panel, null returned
App\Providers\Filament\AdminPanelProvider::panel(): Return value must be of type Filament\Panel, null returned
this is the code:
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->login()
->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
->pages([
Pages\Dashboard::class,
])
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
->widgets([
Widgets\AccountWidget::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
])
->plugin(FilamentSpatieLaravelBackupPlugin::make())
->configureUsing(function (Panel $panel){
dd(Company::first()->brand_color);
$panel->colors([
'primary' => Company::first()->brand_color ?? '#FF6921'
]);
});
}
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->login()
->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
->pages([
Pages\Dashboard::class,
])
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
->widgets([
Widgets\AccountWidget::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
])
->plugin(FilamentSpatieLaravelBackupPlugin::make())
->configureUsing(function (Panel $panel){
dd(Company::first()->brand_color);
$panel->colors([
'primary' => Company::first()->brand_color ?? '#FF6921'
]);
});
}
Lara Zeus
Lara Zeus13mo ago
sorry can you try bootUsing 🙂 ->bootUsing(function (Panel $panel){ dd(Company::first()->brand_color); return $panel->colors([ 'primary' => '#FF6921' ]); }) since its not documented I forget the function name 🙂
LazyBoy
LazyBoyOP13mo ago
it does pass it when i dd the return of bootUsing correctly, but if i dd the whole panel the colors array is empty thank you for taking the time btw appreciate it a lot 😄
Lara Zeus
Lara Zeus13mo ago
np at all, I did something similler in one of the project so I know it would work 🙂 let me check ok this is the code I add id to the app service provider and you have to use Color::hex
FilamentColor::register(function(){
return [
'primary' => Color::hex('#ff0099'),
];
});
FilamentColor::register(function(){
return [
'primary' => Color::hex('#ff0099'),
];
});
also ->colors() dose accept
->colors(function(){
return [
'primary' => Color::hex('#ccdd33')
];
})
->colors(function(){
return [
'primary' => Color::hex('#ccdd33')
];
})
Solution
Lara Zeus
Lara Zeus13mo ago
FilamentColor::register(
[
'primary' => Color::hex('#ff0099'),
]
);
FilamentColor::register(
[
'primary' => Color::hex('#ff0099'),
]
);
LazyBoy
LazyBoyOP13mo ago
if im not wrong it should be added to the boot method right?
Lara Zeus
Lara Zeus13mo ago
ya
LazyBoy
LazyBoyOP13mo ago
oh that worked 😄 thank you so much! Appreciate it a lot!
Lara Zeus
Lara Zeus13mo ago
finally 🙂
LazyBoy
LazyBoyOP13mo ago
been struggling for the past couple of hours haha
DrByte
DrByte13mo ago
@LazyBoy so did you end up simply using FilamentColor::register with bootUsing()? Or did you put it in a ServiceProvider's boot()? Is it still doing the company lookup from db for the color code? (I'm asking because I thought I'd tried in the past to do a query from a ServiceProvider when registering a lifecycle hook and had trouble.)
LazyBoy
LazyBoyOP13mo ago
I put it in the AppServiceProviders boot(), yep I am fetching the color from the db 🙂
leoblanski
leoblanski12mo ago
Hey guys... I need something similar, but I need to get info from the Users table based on User login. Imagine that user are logged in, so i get infos about colors preference and need to set it to PanelProvider, according to what is defined on the database, but when i'm trying to get the user auth inside provider but i dont have access because sessions is not initialized yet ...
LazyBoy
LazyBoyOP12mo ago
I think your best bet in this case is to set a default color and change it only if a user has an active session, otherwise I am not sure if there is a better approach Because in my case im fetching a single company from the database, but it would be tricky if it was like your case with a lot of users
leoblanski
leoblanski12mo ago
Hey @LazyBoy in truth, i need to get the "Team" that user belongs to... (Its a saas platform) I already created a new action and attached it on login to create infos on Session or Cache, but when Panel is starting i have no information about the user auth (because middlewares are called after PanelProvider) to get the infos from cache. as im using redis, i need to set cache with any information about the team... But reading the answers again, i could imagine a solution, if it works return here... <?php namespace App\Http\Middleware; use Closure; use Filament\Facades\Filament; use Filament\Models\Contracts\FilamentUser; use Filament\Support\Facades\FilamentColor; use Filament\Support\Facades\FilamentView; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Symfony\Component\HttpFoundation\Response; class SetTeamSettings { /** * Handle an incoming request. * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle(Request $request, Closure $next): Response { $team = $request->user()?->team; if (!$team) { return $next($request); } FilamentColor::register([ 'primary' => $team?->brand_primary_color, 'secondary' => $team?->brand_secondary_color, ]); return $next($request); } } Now working with brandName and Logo...
Want results from more Discord servers?
Add your server