1

当我试图从公司模型中获取用户详细信息时,我得到了 NULL 值。

class User extends Model
{
  protected $primaryKey = 'uid';
}


class Company extends Model
{
   $table = 'company';

   public function users(){
        return $this->hasMany('App\User','user_id','uid');
   }
}

类回购控制器{

public function index(){
  $user = Company::where('id',1)->with('user')->get(); 
  /// Returning value but without Users
  } 
}`
4

2 回答 2

0

users表应该包含company_id并且您应该定义id为公司 ID,因为company表不使用自定义 ID:

public function users()
{
     return $this->hasMany('App\User', 'company_id', 'id');
}

或者只是这样:

public function users()
{
    return $this->hasMany('App\User');
}

此外,您正在尝试加载user关系,但它应该是users(),所以这样做:

Company::where('id', 1)->with('users')->get();
于 2017-01-25T05:08:49.237 回答
0

我认为你的身份证位置错误

尝试像交换它们一样

return $this->hasMany('App\User','uid','user_id');
于 2017-01-25T07:19:21.930 回答