4

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.

cecilli0n
  • 457
  • 2
  • 9
  • 20
  • It works just like that, unless you have namespaced your model. Where do you run Stack::all()? – Jarek Tkaczyk Apr 25 '14 at 14:11
  • Try running, composer update. Which also runs composer dump-autoload. – majidarif Apr 25 '14 at 14:33
  • @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. Thanks – cecilli0n Apr 25 '14 at 14:44
  • @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 Apr 25 '14 at 14:45
  • Why not just use composer instead? – majidarif Apr 25 '14 at 15:33
  • @majidarif Laravel listed two ways to install. As I do not have composer and have never used it or installed it, it seemed easier to choose the option to get laravel direct. It could also be that if I do get composer the issue stays here, I will probably try composer in a minute as I am getting pretty *f'ed* off. Thanks – cecilli0n Apr 25 '14 at 15:38
  • @cecilli0n using composer will solve this problem as the autoloader is generated by composer itself. I'd recommend reinstalling laravel with composer (ps. it is very easy to install and use) - the windows installer even sets your environment path for you making it even easier. – David Barker Apr 25 '14 at 16:51

3 Answers3

1

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 <taylorotwell@gmail.com>
 */

/*
|--------------------------------------------------------------------------
| 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';
Chris
  • 3,680
  • 6
  • 26
  • 43
0

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

class Stack extends \Eloquent
{


}
Keith Mifsud
  • 1,599
  • 1
  • 16
  • 26
  • Doesn't make any difference, error still the same but thanks anyway. – cecilli0n Apr 25 '14 at 14:48
  • Okay, so it seems like Eloquent doesn't exist. If you've installed laravel using Composer, then I would simply run `composer update` as this will definitely install all dependencies. – Keith Mifsud Apr 25 '14 at 14:52
  • It exists in the directory *\vendor\laravel\framework\src\Illuminate\Database* – cecilli0n Apr 25 '14 at 15:16
  • have you tried composer update? It definitely need to resolve all dependencies and will detail the ones you need. – Keith Mifsud Apr 25 '14 at 15:24
  • I do not have composer, I downloaded laravel.phar direct from laravel.com, I have downloaded direct from http://laravel.com/laravel.phar and set this up twice now. I assume they listed the latest version with all the dependencies covered? – cecilli0n Apr 25 '14 at 15:27
  • sorry I didn't see the previous comment. This should solve the issue with composer update on windows. http://stackoverflow.com/questions/20829129/installing-laravel-4-1-in-windows-7-make-phar-file-globally-available-to-win – Keith Mifsud Apr 25 '14 at 15:39
  • I seeked advice and setup my .bat file via that page originally, however could you tell me which comment you are referring to on that page because some comments are talking about different methods and scenarios. Thanks – cecilli0n Apr 25 '14 at 15:42
  • The approved answer will help you in being able to run composer update. Composer will then let you know if you have missing dependency classe (or if they're broken). – Keith Mifsud Apr 25 '14 at 15:45
  • Updated my original post list, Thanks anyway – cecilli0n Apr 25 '14 at 16:45
0

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 {}
Bastian Hofmann
  • 2,485
  • 19
  • 19
  • I checked *app/config/app.php* and it seems correct, I have in the *aliases* array *'Eloquent' => 'Illuminate\Database\Eloquent\Model',*. As for using the class directly, that will quickly become irritating as I will probably have to do this for every single class and I do not know or wish to verbosely read the documentation right now for how every class is linked to another class to another class and so on. Thanks for the suggestion though. – cecilli0n Apr 25 '14 at 15:23
  • Yes the direct approach was just to figure out where the problem is. – Bastian Hofmann Apr 25 '14 at 15:28
  • With regards to that I tried it but it just returns the same error message but referring to *\Illuminate\Database\Eloquent\Model* not found instead of just *Eloquent* not found, I also updated my original post with what I have tried. – cecilli0n Apr 25 '14 at 15:32