Php 此集合实例上不存在属性[users]

Php 此集合实例上不存在属性[users],php,laravel,eloquent,orm,laravel-6,Php,Laravel,Eloquent,Orm,Laravel 6,这个错误已经在这里发布了好几次,但我遇到了一些不同的情况。我有两个名为users和user\u type的表。而users使用user\u type中的外键。我必须获取所有用户及其类型,我使用Laravel的雄辩ORM来定义关系,这是一对一的关系 用户模型: /** * Get the user type that user has. */ public function users(){ return $this->belongsTo('App\Models\UserType

这个错误已经在这里发布了好几次,但我遇到了一些不同的情况。我有两个名为
users
user\u type
的表。而
users
使用
user\u type
中的外键。我必须获取所有用户及其类型,我使用Laravel的雄辩ORM来定义关系,这是一对一的关系

用户模型

/**
 * Get the user type that user has.
 */
public function users(){
    return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
/**
 * The primary key associated with the table.
 *
 * @var string
 */
protected $primaryKey = 'ut_id';

/**
 * Get the user associated with the user type.
 */
public function users(){
    return $this->hasOne('App\Models\Users', 'user_type_id', $this->primaryKey);
}
$users = Users::all()->users;
用户类型模型

/**
 * Get the user type that user has.
 */
public function users(){
    return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
/**
 * The primary key associated with the table.
 *
 * @var string
 */
protected $primaryKey = 'ut_id';

/**
 * Get the user associated with the user type.
 */
public function users(){
    return $this->hasOne('App\Models\Users', 'user_type_id', $this->primaryKey);
}
$users = Users::all()->users;
取数控制器

/**
 * Get the user type that user has.
 */
public function users(){
    return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
/**
 * The primary key associated with the table.
 *
 * @var string
 */
protected $primaryKey = 'ut_id';

/**
 * Get the user associated with the user type.
 */
public function users(){
    return $this->hasOne('App\Models\Users', 'user_type_id', $this->primaryKey);
}
$users = Users::all()->users;
根据,我可以将此方法作为属性访问,但它向我显示了定义的错误。我也尝试将其作为一种方法访问,但它说:

方法Illumb\Database\Eloquent\Collection::users不存在

我还尝试通过
join()
获取它们,但它只返回少数用户,我不知道为什么:

$users = Users::where('id', '>', '0')
        ->join('user_type', 'user_type.ut_id', '=', 'users.id')
        ->select([
            'user_type.ut_name',
            'users.*'
        ])->get();
有人能告诉我我做错了什么吗


p.s:我只想在用户模型中显示所有用户及其各自的类型

public function type(){
   return $this->hasOne(UserType::class, 'id');
}
public function users(){
  return $this->belongsToMany(User::class, 'id');
}
在用户类型模型中

public function type(){
   return $this->hasOne(UserType::class, 'id');
}
public function users(){
  return $this->belongsToMany(User::class, 'id');
}

你们的关系似乎是错的

用户链接到id为ut_id的用户类型,但用户类型链接到id为User_type_id的用户

我很确定用户类型应该是这样的

/**
 * Get the user associated with the user type.
 */
public function users(){
    return $this->hasMany('App\Models\Users', 'id', 'user_type_id');
}
然后这是给用户的

public function userTypes(){
    return $this->belongsTo('App\Models\UserType', 'user_type_id', 'id');
}
然后你可以渴望加载所有你想要的结果

$users = Users::where('id', '>', '0')
        ->with('userTypes')
        ->get();

您错过了users表和usertypes表之间的确切外键

首先,您定义了用户表的外键是基于您的belongsTo关系的“ut_id”。在这个问题上

/**
 * Get the user type that user has.
 */
public function users(){
    return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
/**
 * The primary key associated with the table.
 *
 * @var string
 */
protected $primaryKey = 'ut_id';

/**
 * Get the user associated with the user type.
 */
public function users(){
    return $this->hasOne('App\Models\Users', 'user_type_id', $primaryKey);
}
第二个是,在用户类型模型中,您对名为“user\u type\u id”的用户表使用了一个外键,首先在users表中将其命名为“ut\u id”。在这个问题上

/**
 * Get the user type that user has.
 */
public function users(){
    return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
/**
 * The primary key associated with the table.
 *
 * @var string
 */
protected $primaryKey = 'ut_id';

/**
 * Get the user associated with the user type.
 */
public function users(){
    return $this->hasOne('App\Models\Users', 'user_type_id', $primaryKey);
}
您必须匹配用于解决问题的外键

现在,为了获取所有用户及其类型,您的查询应该如下所示

$users = Users::with('users')->get();
假设您的用户表具有此关系

public function users(){
    return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
你的用户类型模型有这样的关系

public function users(){
    return $this->hasOne('App\Models\Users', 'ut_id', $this->primaryKey);
}

这可能与问题本身没有直接关系,但您应该省略
$This->hasOne()
的第三个参数,或者传递
$This->primaryKey
以正常工作,对吗?是的,您是对的,它应该是
$This->primaryKey
。我已经更新了我的问题。好的,我已经像你提到的那样切换了这些键,它们在前面被定义为错误的。但问题仍然是一样的。它现在通过
$users=users::with('users')->get()获取所有数据
但是当我尝试这样获取时,它不知怎么地向我显示了错误:
$users=users::all()->users。它有什么问题?你不能直接呼叫它的用户,因为laravel不知道你从哪里获取用户属性。要使用它的属性,你必须像User::find($id)->users那样操作如果这个答案对你有帮助,请将它标记为answer请注意find()只返回一个结果,你在问题中说你需要所有用户。关于如何实现这一点,请参见下面我的答案