【发布时间】:2015-07-31 19:17:45
【问题描述】:
我有这两种型号:
<?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”
我不知道我在这里做错了什么。
【问题讨论】:
标签: laravel laravel-5 eloquent models