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

satheeshkumarj's avatar

How to call a function from view

I need to call a function from view. My controller PushNotificationController

public function viewNotification(){
            $users = auth()->user();
            $user = User::find($users->id); 
            $notification = $user->notifications;
            $notifications = array(); 
			return $notifications;
        }

In my view

{{
use App\Http\Controllers\PushNotificationController;
$notifications = PushNotificationController::viewNotification();}}

But error showing Non-static method App\Http\Controllers\PushNotificationController::viewNotification() cannot be called statically

0 likes
5 replies
RayC's avatar

Your method is not a static method. To make it static you need to specify that:

public static function viewNotification(){
    $users = auth()->user();
    $user = User::find($users->id); 
    $notification = $user->notifications;
    $notifications = array(); 
    return $notifications;
}

But why are you calling it in the view in the first place? You should call it in the Controller method you are displaying the view from.

You can also shorten teh method you're using:

public static function viewNotification(){
    $user = User::find(auth()->user()->id); 
    $notification = $user->notifications;
    $notifications = array(); 
    return $notifications;
}
1 like
satheeshkumarj's avatar

@RayC I need to use this $notification in header.blade which is common in all pages. So it not easy to write this function in each and every controller

Sinnbeck's avatar

Can you explain why you want to call a controller function from a view? I cannot think of a reason for this? Just send the data to the view from the controller, or use a component

satheeshkumarj's avatar

@Sinnbeck I need to use this $notification in header.blade which is common in all pages. So it not easy to write this function in each and every controller

Please or to participate in this conversation.