简体   繁体   English

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

[英]Property [users] does not exist on this collection instance

This error has been posted here several times but I'm encountering something a little different.此错误已在此处发布多次,但我遇到了一些不同的情况。 I have two Tables named users and user_type .我有两个名为usersuser_type表。 And users uses a foreign key from user_type . users使用来自user_type的外键。 I have to fetch all users and their types, I'm using Laravel's Eloquent ORM to define relationships, this is a one to one relation.我必须获取所有用户及其类型,我使用 Laravel 的 Eloquent ORM 来定义关系,这是一对一的关系。

Users Model :用户型号

/**
 * Get the user type that user has.
 */
public function users(){
    return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}

UserType Model :用户类型模型

/**
 * 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);
}

Fetching Controller :获取控制器

$users = Users::all()->users;

According to Laravel ORM one-to-one I can access this method as a property, but it's showing me the defined error.根据Laravel ORM 一对一,我可以将此方法作为属性访问,但它向我显示了定义的错误。 I've also tried to access it as a method but it's saying:我也尝试将它作为一种方法访问,但它说:

Method Illuminate\\Database\\Eloquent\\Collection::users does not exist.方法 Illuminate\\Database\\Eloquent\\Collection::users 不存在。

I've also tried to fetch them by join() but it's returning only a few users, I don't know why:我也尝试通过join()获取它们,但它只返回少数用户,我不知道为什么:

$users = Users::where('id', '>', '0')
        ->join('user_type', 'user_type.ut_id', '=', 'users.id')
        ->select([
            'user_type.ut_name',
            'users.*'
        ])->get();

Can someone tell me what I'm doing wrong?有人能告诉我我做错了什么吗?

Ps : I just want to show all the users with their respective types Ps :我只想显示所有用户各自的类型

You had missed the exact foreign key between your users table and usertypes table.您错过了用户表和用户类型表之间的确切外键。

First, you defined the that the foreign key of your user table is 'ut_id' base of what you had in your belongsTo relationship.首先,您定义了用户表的外键是您的belongsTo 关系中的“ut_id”基础。 On this one在这一

/**
 * Get the user type that user has.
 */
public function users(){
    return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}

Second is that, in your user type model, you used a foreign key to user table named 'user_type_id', which is at first you named it as 'ut_id' in your users table.其次,在您的用户类型模型中,您使用了名为“user_type_id”的用户表的外键,最初您在用户表中将其命名为“ut_id”。 On this one在这一

/**
 * 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);
}

You have to match this foreign keys you used to solve your problem.您必须匹配用于解决问题的外键。

Now, to fetch your all user with their types, your query should look like this.现在,要获取所有用户的类型,您的查询应如下所示。

$users = Users::with('users')->get();

assuming that your user table has this relationship假设您的用户表具有这种关系

public function users(){
    return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}

and your user types model has this relationshio并且您的用户类型模型具有这种关系

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

in User model在用户模型中

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

in UserType Model在用户类型模型中

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

Your relations seem to be wrong.你们的关系似乎不对。

Users links to UserType with id to ut_id, but userType links to User with id to user_type_id用户链接到 id 为 ut_id 的 UserType,但 userType 链接到 id 为 user_type_id 的用户

I'm pretty sure that it should be this for userTypes我很确定它应该是 userTypes

/**
 * Get the user associated with the user type.
 */
public function users(){
    return $this->hasMany('App\Models\Users', 'id', 'user_type_id');
}

and then this for Users然后这对于用户

public function userTypes(){
    return $this->belongsTo('App\Models\UserType', 'user_type_id', 'id');
}

Then you can eager load for all the results you want...然后你可以急切地加载你想要的所有结果......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM