Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iamamirsalehi's avatar

View composer is not working

Hey there, I'm using View composer for passing some data to the master layout but it actually passes nothing to the view, What's the problem?

 View::composer('layouts.dashboard.master', function($view){
            $userNoteRepository = resolve(UserNoteRepositoryInterface::class);
     
            $current_user_notification       = $userNoteRepository->getCurrentUserAlertNotifications();
            $current_user_notification_count = count($userNoteRepository->getCurrentUserAlertNotifications());

            $view->with('current_user_notification', 'current_user_notification_count');
        }); 

thanks in advance

0 likes
10 replies
neilstee's avatar
neilstee
Best Answer
Level 34

@iamamirsalehi I think the problem is on with? it should be:

$view->with([
	'current_user_notification' => $current_user_notification,
	'current_user_notification_count' => $current_user_notification_count
]);
1 like
neilstee's avatar

@iamamirsalehi if this solves your issue don't forget to mark it as "Best Answer" so someone can find it useful as well 😉

abrada's avatar

Ok, where you put this code? In service provider in method boot()?

For tests, keep your code simple

View::composer('layouts.dashboard.master', function($view){
  $view->with('param', 100);
}); 

and check

iamamirsalehi's avatar

@neilstee @traster100 thanks all, the syntax should be like this

            $view->with(['current_user_notification' => $current_user_notification,  'current_user_notification_count' => $current_user_notification_count]);


Please or to participate in this conversation.