Class 未找到 - Eloquent
Class Not Found - Eloquent
我有这两个型号:
<?php namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password', 'is_active'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function customer_details()
{
return $this->hasOne('CustomerDetails', 'user_id');
}
}
并且:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CustomerDetails extends Model {
protected $table = 'customer_details';
public function user()
{
return $this->belongsTo('User');
}
}
现在我试图从我的 UserController() 的 index() 数据库中获取所有客户及其用户数据:
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$users = User::with('customer_details')->get();
return [
'users' => $users
];
}
但是我一直收到这个错误:
Fatal Error Exception Class 'CustomerDetails' not found
我不知道我做错了什么。
您的 class 已命名空间,因此在 App\Models\User
模型的 customer_details
的 $this->hasOne(...)
定义中应称为 App\Models\CustomerDetails
。
我有这两个型号:
<?php namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password', 'is_active'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function customer_details()
{
return $this->hasOne('CustomerDetails', 'user_id');
}
}
并且:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CustomerDetails extends Model {
protected $table = 'customer_details';
public function user()
{
return $this->belongsTo('User');
}
}
现在我试图从我的 UserController() 的 index() 数据库中获取所有客户及其用户数据:
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$users = User::with('customer_details')->get();
return [
'users' => $users
];
}
但是我一直收到这个错误:
Fatal Error Exception Class 'CustomerDetails' not found
我不知道我做错了什么。
您的 class 已命名空间,因此在 App\Models\User
模型的 customer_details
的 $this->hasOne(...)
定义中应称为 App\Models\CustomerDetails
。