Thomas Kaemmerling
Thomas Kaemmerling

Reputation: 547

Laravel can't find class in namespace

I using Laravel, I have a Model class under App/Models

<?php
namespace App\Models;

class TodoList extends \Eloquent{
  public function listItems(){
      return $this->hasMany('TodoItem');
  }
}

In my Controller I have included the namespace as follows:

namespace App\Http\Controllers;

...

use App\Models\TodoItem;
use App\Models\TodoList;

class TodoListController extends Controller

My method looks like this:

public function show($id)
{
    $list=TodoList::findOrFail($id);
    return \View::make('todos.show')->with('list', $todo_list);
}

but when I call to a request i get the error:

FatalErrorException in TodoListController.php line 75: Class 'App\Models\TodoList' not found

Upvotes: 4

Views: 9125

Answers (4)

J.McLaren
J.McLaren

Reputation: 641

Trying running composer dump-autoload. Basically your classes become cached so you need to tell Laravel to look for newly added classes.

Upvotes: 6

Christian Cromnow
Christian Cromnow

Reputation: 1

Make sure the file is in the correct folder and has the same name caption as the Class you want to import.

If the caption of the file "TodoList" is not TodoList.php but Todolist.php for example, Laravel wont find it.

Depending on your setup running "composer dump" might help to refresh autoload files.

Upvotes: 0

Thomas Kane
Thomas Kane

Reputation: 68

Here is the code from the docs, but for your example. Note the use Model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class TodoList extends Model {

    //insert public function listItems() here

}

Hope this is helpful!

Upvotes: 1

Kiran LM
Kiran LM

Reputation: 1379

I'm not sure, just try this :

namespace App\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;
class TodoList extends Eloquent{
  public function listItems(){
      return $this->hasMany('TodoItem');
  }
}

Upvotes: 1

Related Questions