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'];
}
?>
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
There was nothing wrong with the code, I just used
composer dump-autoload
in CMD and it worked fine :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.