Eloquent class not found

Fairly new to eloquent, and trying to echo some test users but I get this error:

Fatal error: Class 'User' not found in E:\XAMPP\htdocs\Social\php\user-query.php on line 3

Any idea why it's not working ?

php/user-query.php:

<?php
include 'db-connection.php';
$user = User::all();
foreach($user as $u) {
  echo $u['id'];
}
?>

php/User.php:

<?php
use Illuminate\Database\Eloquent\Model;
require_once './vendor/autoload.php';

class User extends Model
{
  public $table = "user";
  protected $fillable = ['id', 'user_name', 'first_name', 'last_name', 'email'];
}
?>

Upvotes: 1

Views: 1100

Answers (2)

There was nothing wrong with the code, I just used

composer dump-autoload

in CMD and it worked fine :)

Upvotes: 2

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

Inside your file php/user-query.php add this at the top:

use Illuminate\Database\Eloquent\Model\User;
require_once './vendor/autoload.php';

You need to tell to php where to find the class User to use It

Upvotes: 1

Related Questions