F
Filament7mo ago
Jakub

accessing auth user in panel builder to make custom navigation

I am trying to generate a custom dashboard navigation for users based on the projects they have. So it's quick access from the sidebar. Or if they have pinned tasks ect. In the panel provider I added
->navigationItems(
collect(Project::where('workspace_id', auth()->user()->workspace_id)->get())->map(function ($project) {
return NavigationItem::make($project->name)
->url('/agency/projects/' . $project->id)
->icon('heroicon-o-briefcase')
->group('Projects');
})->toArray()
)
->navigationItems(
collect(Project::where('workspace_id', auth()->user()->workspace_id)->get())->map(function ($project) {
return NavigationItem::make($project->name)
->url('/agency/projects/' . $project->id)
->icon('heroicon-o-briefcase')
->group('Projects');
})->toArray()
)
I keep getting error
Attempt to read property "workspace_id" on null
Attempt to read property "workspace_id" on null
When I just do Project::all() it does generate the items correctly.
Solution:
Solution for future people Make new middleware ``` php artisan make:middleware AddWorkspaceNavigationItems...
Jump to solution
4 Replies
Jakub
JakubOP7mo ago
tytyty wow got it working with that one
Solution
Jakub
Jakub7mo ago
Solution for future people Make new middleware
php artisan make:middleware AddWorkspaceNavigationItems
php artisan make:middleware AddWorkspaceNavigationItems
add the middleware to the panel provider
->middleware([

AddWorkspaceNavigationItems::class,
])
->middleware([

AddWorkspaceNavigationItems::class,
])
How the middleware is setup
<?php

namespace App\Http\Middleware;

use Closure;
use App\Models\Project;
use Illuminate\Http\Request;
use Filament\Navigation\NavigationItem;
use Symfony\Component\HttpFoundation\Response;
use Filament\Navigation\MenuItem;

class AddWorkspaceNavigationItems
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (!auth()->check()) {
return $next($request);
}

$workspaceId = auth()->user()->workspace_id;
$projects = Project::where('workspace_id', $workspaceId)->get();

$items = [];
foreach ($projects as $project) {
$items[] = NavigationItem::make()
->label($project->name)
->url('/agency/projects/' . $project->id)
->icon('heroicon-o-briefcase');
}

filament()->getCurrentPanel()->navigationItems($items);

return $next($request);
}
}
<?php

namespace App\Http\Middleware;

use Closure;
use App\Models\Project;
use Illuminate\Http\Request;
use Filament\Navigation\NavigationItem;
use Symfony\Component\HttpFoundation\Response;
use Filament\Navigation\MenuItem;

class AddWorkspaceNavigationItems
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (!auth()->check()) {
return $next($request);
}

$workspaceId = auth()->user()->workspace_id;
$projects = Project::where('workspace_id', $workspaceId)->get();

$items = [];
foreach ($projects as $project) {
$items[] = NavigationItem::make()
->label($project->name)
->url('/agency/projects/' . $project->id)
->icon('heroicon-o-briefcase');
}

filament()->getCurrentPanel()->navigationItems($items);

return $next($request);
}
}
Jakub
JakubOP7mo ago
No description
Want results from more Discord servers?
Add your server