4
votes

I am trying to use Laravel and have been following the official Laravel Eloquent documentation and multiple tutorials at credible sources tuts plus

I created a model inside app/models called Stack with a table in the database called stacks with a primary key column called id, as corresponding to Laravels defaults.

<?php (Stack.php)

class Stack extends Eloquent
{


}

$stacks = Stack::all();

However when I run this model I get the following error message.

Fatal error: Class 'Eloquent' not found in C:\www\laravelproject\app\models\Stack.php on line 4

Including the official documentation and the reputable tutorials, I have also watched 2 youtube tutorials and it seems like there is no additional autoloading/including/requiring required to be declared in any new defined model's, so I am assuming something else here maybe wrong.

Do I have to manually find all classes I must autoload? If so, why is this not written in the official documentation?

I downloaded the latest laravel.phar file directly from laravel and used a .bat file to call it. (Not via composer)

Some things I have checked/tried to fix the problem.

  • Eloquent directory does exist at vendor\laravel\framework\src\Illuminate\Database\Eloquent
  • Eloquent alias set in app/config/app.php. Default 'Eloquent' => 'Illuminate\Database\Eloquent\Model'
  • Directly extending class like \Illuminate\Database\Eloquent\Model, error message the same but with \Illuminate\Database\Eloquent\Model instead of just Eloquent
  • Tried to directly extend through all variations by navigating down the entire Laravel directory structure \vendor\laravel\framework\src\Illuminate\Database\Eloquent, then \laravel\framework\src\Illuminate\Database\Eloquent etc... etc...
  • Bit the bullet and decided to try the second official method, I installed composer and ran the command composer create-project laravel/laravel --prefer-dist, the command screen alerted me it was downloading files which was then all successful at 100%, then alerted me that a generated application key was set successfully. I then navigate to the new directory model/User.php and receive the exact same error message as when I did it with the previuos method(laravel.phar direct download).

Thanks in advance.

3
It works just like that, unless you have namespaced your model. Where do you run Stack::all()?Jarek Tkaczyk
Try running, composer update. Which also runs composer dump-autoload.majidarif
@deczo I ran Stack::all() temporarily within the model file. As for namespaces I havnet done anything apart from call new laravel laravelproject and created a model file. Thankscecilli0n
@majidarif I did not use composer, I used laravel.phar(direct latest download) and then created a windows .bat script to call it. I will update my original to include this.cecilli0n
Why not just use composer instead?majidarif

3 Answers

1
votes

Make sure you are accessing the application from the correct 'entrance'.

Thus, accessing it from app/public/index.php.

The app/public/index.php file loads the autoloader.

<?php
/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <[email protected]>
 */

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/

require __DIR__.'/../bootstrap/autoload.php';
0
votes

It's possible your is namespacing. Try adding the backslash before the class being extended.

class Stack extends \Eloquent
{


}
0
votes

Make sure your are setting the Eloquent alias in the app config. (app/config/app.php)

Alternatively use the class directly. I believe it's: Illuminate\Database\Eloquent\Model

class Stack extends \Illuminate\Database\Eloquent\Model {}