My Controller for my class 'Article' cannot be found.
I am needing to fetch all entries from the article table.
I am able to use the DB:: facade to pull what's in the DB, but when I try using Article::all() I get:
Class 'App\Http\Controllers\Article' not found
in ArticleController.php line 15
at Application->Laravel\Lumen\{closure}()
Line 15 looks like:
$article = Article::all();
This is what I've tried so far, but with no success:
updated .env.example to .env and set up my DB credentials.
in bootstrap/app.php I've uncommented Dotenv::load(__DIR__.'/../');
In bootstrap/app.php I've uncommented $app->withFacades(); $app->withEloquent();
I've tried to use the full route of the controller in routes.php: $app->get('article', 'App\Http\Controllers\ArticleController#index');
My model directory is located under app->Models and has my Article.php model:
<?php
# app/Models/Article.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $table = 'articles';
protected $fillable = ['title', 'content'];
}
My Controller is ArticleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ArticleController extends Controller{
public function index(){
$article = Article::all();
return response()->json($article);
}
}
And my routes.php
<?php
$app->get('article', 'ArticleController#index');
I really appreciate any help on this error. I've unfortunately spent the better part of 2 days on this.
Thanks.
You need to have the proper namespace. Your Article model is in the App\Models namespace so you need to add this to the top of your controller:
use App\Models\Article;
Just run composer update. a new autoload classmap will be generated and all your new composer will be ready to autoload after successful update.
Thanks for your responses and for your answer #Thomas. I followed your response and was able to add the correct namespace in my controller.
Related
I am very new in Laravel and this is my first project. I have this problem and I don`t know how to fix it.
This is my directory
I am sure I have tried all I know
This is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class EditareProfilController extends Controller
{
public function __construct()
{
$tasdhis->middlewasdare('auasdth');
}
public function index()
{
return vasdasdiew('editaasdasdasdeprofil');
}
}
and this is my route:
Route::get('/editarasdasdeprofil', 'EditareProfilasdasdasdontroller#index')->name('editarepasdasdasdrofil');
The default location for controllers is app\Http\Controllers if you add a folder just add it to your route in order to laravel localize it. In your code you are storing your controller under the Auth folder.
Update your route to:
Route::get('/editareprofil', 'Auth\EditareProfilController#index')->name('editareprofil');
And also don't forget to update the namespace.
namespace App\Http\Controlllers\FOLDER;
Where to save new model file created in laravel 5.3 in /app/http folder or in /app folder
And how I will call the method in it from controller ?
How to include the model in controller?
Laravel 5.3
Save them in app/models folder and you can access it by constucting it,Make sure you have ran composer dump-autolad
public function __construct(YourModel $model) {
parent::__construct();
$this->model = $model;
}
in your code simply model->test();it should work.
You create the folder Modelswherever you want and add your files as you wish, just make sure to properly namespace it. As long as Composer can autoload the class.
You can manually create app/Models/User.php and just make sure to namespace it:
namespace App\Models;
class User {
// your code
}
And to call the model method from a controller use the namespace of the model:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\YourModel;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class YourModelController extends Controller
{
public function whatever(){
$yourmodel->method();
}
}
I'm learning laravel but it isn't working out that well....
I've set my route in routes.php:
Route::get('/','WelcomeController#index');
Then I obviously have made a controller called "WelcomeController" and it looks like this:
<?php
class WelcomeController extends BaseController
{
public function index()
{
return view ('index');
}
}
?>
And then I've made a view called index with just some html text.
But when I go to localhost/public I receive the error:
FatalErrorException in WelcomeController.php line 3:
Class 'BaseController' not found
And when I say:
class WelcomeController extends Illuminate\Routing\Controller
It does not work!
What am I doing wrong.
You should try
use Illuminate\Routing\Controller as BaseController;
at the top of your controller file. That acts as an import
Two suggestions:
Run php composer dump-autoload to make sure the class mappings is fresh.
Add use Controller; in your use block. The modify your controller to extend it. Example:
class WelcomeController extends Controller {...
Controller is an interface in Laravel 4.*
In Laravel 5 use instead:
use App\Http\Controllers\Controller; according to the documentation here: http://laravel.com/docs/5.0/controllers
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
Trying running composer dump-autoload. Basically your classes become cached so you need to tell Laravel to look for newly added classes.
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');
}
}
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!
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.
I have created a model using
php artisan make:model Sessions
which creates a model named Sessions in the App\ folder. My Model (Sessions) and the controller(School) calling the model are below
Model (Sessions):
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Sessions extends Model {
//
protected $table = "sch_sessions";
protected $primaryKey = "session_id";
}
Controller (School)
<?php
namespace App\Http\Controllers;
use App;
class SchoolController extends Controller {
public function sessionManagement(){
$Sessions = Sessions::all();
}
}
I get the following error
FatalErrorException in SchoolController.php line 7: Class
'App\Http\Controllers\Session' not found
I've tried putting the model in a folder and "use"ing it but nada. I'm stumped as to why the error is referring to a controller in the first place. Can anyone see something I don't (can't) please?
add on the top
use App\Sessions;
or use it with prefix
\App\Sessions::all();
If user Ali Mohammed's answer doesn't clear it up for you, it looks to me like your error is happening elsewhere. App\Http\Controllers\Session note Session is not plural.
Are you attempting to use the Session class elsewhere in your controller? If so, make sure to prefix it so it's using the Session declared in the global namespace. You can do this with \Session.