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
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?
21 Replies
there is a function
configureUsing
not sure if it documented
maybe you can use it to fetch the color and set it as primaryI 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
you can chane it to the $panel
see if the dd works
It is throwing an error and returning null
what is the error?
this is the code:
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 🙂
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 😄
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
also ->colors()
dose accept
Solution
if im not wrong it should be added to the boot method right?
ya
oh that worked 😄
thank you so much! Appreciate it a lot!
finally 🙂
been struggling for the past couple of hours haha
@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.)I put it in the AppServiceProviders boot(), yep I am fetching the color from the db 🙂
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 ...
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
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...