Ifrit's avatar

Class 'Menu' not found in helper.php

I've got a helper.php where I put code that I plan on reuseing. I'm trying to get a function to work but I'm getting this error

FatalErrorException in helpers.php line 90: 
Class 'Menu' not found

This is my code

function getSeoLink($page_id)
    {

        $menu = Menu::where('id', $page_id)->firstOrFail();

        foreach($menu->seo as $seo){
            $seo = $seo->url;
        }

        if(empty($seo)){
            return '/'.$page_id;
        }

        if(!empty($seo)){
            $url = (!empty($seo) ? '/'.$seo : '/'.$page_id);

            return $url;
        }

        return $url;
    }

and line 90 is

$menu = Menu::where('id', $page_id)->firstOrFail();
0 likes
5 replies
christopher's avatar

Did you imported your Menu Model in the top of your Controller? e.g

// or what ever the Namespace of Menu is
use App\Menu 
....
SaeedPrez's avatar
Level 50

Just a small addition to @christopher solution, just as an alternative.

$menu = App\Menu::where('id', $page_id)->firstOrFail();

I prefer this if I'm only referring that class once or twice, but if I need it more, I go with the use App\Model

Ifrit's avatar

@Prez - I've got another problem similar to this one but this one has to do with the html function. This is my code

Html::macro('clever_link', function($route, $text){
        if(Request::path() == $route){
            $active = "class = 'active'";
        }else{
            $active = '';
        }

        return '<li '.$active. '>'.link_to($route, $text).'</li>';
    });

I did try adding a path to it like I did for the Menu class but that one didn't work

SaeedPrez's avatar

@Ifrit

Your Menu class is in your app/ folder, and if you edit the file you will find namespace App, hence the App\Menu.

Locate the Html class and edit the file, look at the namespace, add the namespace before the model name, i.e. My\Namespace\Html::macro()

Please or to participate in this conversation.