YVS1102
YVS1102

Reputation: 2748

Class "MenuModel" not Found

I get this error when i try to run my Laravel

Class 'Menumodel' not found in HasRelationships.php (line 487)

Here is my data structure

enter image description here

And this is MainComposer.php

<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use App\Menumodel as menu;

class MainComposer
{
    public $items = [];

    public function __construct()
    {
         $this->items = menu::tree();
    }

    public function compose(View $view)
    {
        $view->with('items', end($this->items));
    }
}

MenuModel

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

    class Menumodel extends Model
    {
        //
         public $table = 'Menu';
         protected $fillable = ['MenuParent','MenuName','MenuPath','MenuIcon','MenuOrder','RouteName'];

         public function parent() {
            return $this->hasOne('Menumodel', 'MenuCode', 'MenuParent');
        }
        public function children() {
            return $this->hasMany('Menumodel', 'MenuParent', 'MenuCode');
        }  
        public static function tree() {
            return static::with(implode('.', array_fill(0, 4, 'children')))->where('MenuParent', '=', NULL)->get();
        }

    }

I aldredy try this use \App\Menumodel as menu; but still no different. How can i fix it ?

Upvotes: 0

Views: 108

Answers (1)

fubar
fubar

Reputation: 17398

Your relationships are incorrect. You need to provide the full class namespace.

return $this->hasOne(Menumodel::class, '...', '...');
return $this->hasMany(Menumodel::class, '...', '...');

I've also removed your local and foreign keys because if using Laravel, these are typically snake_case, not StudlyCase, so you may need to double check those too.

Upvotes: 1

Related Questions