FilamentAsset::registerScriptData ? #9538
Unanswered
schlaumeier2023
asked this question in
Help
Replies: 3 comments 2 replies
-
|
You are right. You could send a PR to suggest another example |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
You can get this working, by creating a new Middleware: <?php
namespace App\Http\Middleware;
use Closure;
use Filament\Support\Facades\FilamentAsset;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class JSFilamentData
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
// Add data to javascript from filament
FilamentAsset::registerScriptData([
'user' => auth()->user()->only('id', 'name'),
]);
return $next($request);
}
}You then need to register the middleware in your panel provider such as AdminPanelProvider (depending on if you have multiple ones etc). ->authMiddleware([
Authenticate::class,
\App\Http\Middleware\JSFilamentData::class,
])You need to ensure that your new middleware is after the Authenticate class to ensure it is loaded after. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
use this on AppServiceProvider > boot() use Illuminate\Auth\Events\Authenticated;
use Illuminate\Support\Facades\Event;
Event::listen( Authenticated::class, function ( $event ) {
FilamentAsset::registerScriptData( [
'user' => [
'id' => $event->user?->id,
'name' => $event->user?->name,
],
] );
} ); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
https://filamentphp.com/docs/3.x/support/assets#registering-script-data
In this section it is written, we can register script data in the boot function of AppServiceProvider, and as an example it is relating to auth()->user()
However, as my experience in this stage of code, user() is always null and not initializied yet.
Beta Was this translation helpful? Give feedback.
All reactions