0
votes

I am trying to make Multi-Level Category using laravel but I am facing this error: How to fix this error?

Illuminate\Database\QueryException

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'storykadb.category_navmenus' doesn't exist (SQL: select * from category_navmenus where p_id = 0)

enter image description here

Model

namespace App;
use Illuminate\Database\Eloquent\Model;
class category_navmenu extends Model
{
    public function childs(){
       return $this->hasMany('App\category_navmenu','p_id');
    }
}

Route

Route::get('test',function(){
   return App\category_navmenu::with('childs')->where('p_id',0)->get();
});
2
Please rename your table category_navmenus from category_navmenu. It will automatically resolve your issue. - Rahul Rathore

2 Answers

2
votes

Laravel Eloquent is taking the wrong table name, either you can change your Model name and table name according to Laravel's naming convention or add in the $table property in your model, like this :

namespace App;
use Illuminate\Database\Eloquent\Model;

class category_navmenu extends Model
{
  public $table = "category_navmenu";

  public function childs(){

    return $this->hasMany('App\category_navmenu','p_id');
  }
}
0
votes

Because Laravel will find your table by changing your classname to lowercase underscore and plural. Just like: CategoryNavmenu will become category_navemenus.

You have no specifies the table name of the protected $table. So laravel will find the table by default rule.

You can put this line in your model:

  protected $table = 'category_navemenu';

so laravel can find your table.

But the important thing is plz change your class name to Upper Camel Case。This is a normal grammatical norms.

Just Change your model file name to CategoryNavmenu.php and classname to CategoryNavmenu.