iconic404
iconic404

Reputation: 1

How to call a function from another route in laravel php

im trying to call a function from another route in laravel php.

I have the route "welcome"

Route::get('/', function () {
    return view('welcome');
});

where im calling this extends (sidebar)

@extends('layouts.guestnavbar')

i want to see if my table notifications is empty or not so i made this controller

class GuestNavbarController extends Controller
{
    public function isempty(){
        $notif = Notification::first();
        if(is_null($notif)) {
            return view('layouts.guestnavbar')->with("checkempty", "empty");
        }else {
            return view('layouts.guestnavbar')->with("checkempty", "not empty");
        }
    }
}

and i called the variable {{ $checkempty }} in my route guestnavbar

Route::get('/guestnavbar', [GuestNavbarController::class, 'isempty']);

and it works when im in the route guestnavbar but doesnt work when im in the route welcome because i call the function in the route guestnavbar and in welcome he doesnt recognize the variable: checkempty

i need this function to be in the guestnavbar because i have to call it on other pages, not just in welcome page

I appreciate any help.

Upvotes: 0

Views: 755

Answers (1)

Milan Mijatovic
Milan Mijatovic

Reputation: 156

You don't need isempty inside controller, just add method isempty inside Notification model, you can use something like this inside Notification model:

public static function isEmpty(){
    return Notification::first() ? true : false;
}

And then where you need to check if notification table is empty just call Notification::isEmpty()

Upvotes: 2

Related Questions