Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4886

Laravel 5 Class 'App\Http\Controllers\Category' not found

guys following the tuts tutorial for laravel , Since I have installed Laravel 5 couple of errors are throwing .

Code

public function getIndex() {
        return View::make('categories.index')
            ->with('categories', Category::all());
    }

This function is throwing two errors

Class 'App\Http\Controllers\Category' not found
Class 'App\Http\Controllers\views' not found

Well I know this is because of the different name spacing in Laravel 5 . for the 2nd error I tried to add

use view at the begining of the file but it is not finding the view . Can any one let me know what directory these files resides

Thanks

Upvotes: 3

Views: 6469

Answers (2)

Cayce K
Cayce K

Reputation: 2338

Both of those are not in your Controllers. The View class is apart of Illuminate and the other one is most likely a class Model you created.

When you use a new class in a php file you must include it or use it.

The way you would most likely do this for your Category class is"

use App\Category that should solve that one.

Your view one is only slightly more difficult. If you use an IDE where you can import classes that is much easier to do so you don't have to remember the namespace / class name. However, if you don't you need to know which one to use and when to use it. In this case you will need to do:

use View this should solve this.

So in your controller above the declaration and below your namespace you must put the use calls

<?php namespace App\Http\Controllers

// I'm removing this because there is a way not to even have to use this...
// use View;
use App\Category;

class YourController extends Controller {
    ...
}

Like I mentioned in my comment there is a better way. You could just use the helper function view() to return the view just the same without doing the view the way you are doing it.

All you have to change is:

return View::make('categories.index')->with('categories', Category::all());

to

return view('categories.index', ['categories' => Category::all()]);
    or
return view('categories.index')->with('categories',Category::all())

Removes the confusion of messing up the classes.

Upvotes: 3

pinkal vansia
pinkal vansia

Reputation: 10310

add following at top of file just below namespace declaration,

use Illuminate\Support\Facades\View;
use App\Category;

Upvotes: 2

Related Questions