我有这两个模型:
<?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
];
}
但我一直收到这个错误:
未找到致命错误异常类'CustomerDetails'
我不知道我在这里做错了什么。
答案 0 :(得分:1)
Your class is namespaced and should therefore be referred to as App\Models\CustomerDetails
, in the $this->hasOne(...)
definition of customer_details
of the App\Models\User
model.