【问题标题】:Laravel eloquent model relationshipLaravel 雄辩的模型关系
【发布时间】:2019-02-12 19:38:46
【问题描述】:

我的应用基于

Laravel: 5.6.35
PHP 7.2.4
Entrust: 1.9

我的榜样

class Role extends EntrustRole
{
    public function permissions()
    {
        return $this->belongsToMany(Permission::class);
    }

    public function users()
    {
        return $this->hasMany(User::class);
    }
}

我的用户模型是

class User extends Authenticatable
{
    public function role()
    {
        return $this->belongsTo(Role::class);
    } 
}

现在你可以在 Tinker 中注意到了

D:\work\www\myapp>php artisan tinker
Psy Shell v0.9.7 (PHP 7.2.4 — cli) by Justin Hileman
>>> App\models\Role::find(1)->users()->get()
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.role_id' in 'where clause' (SQL: select * from `users` where `users`.`role_id` = 1 and `users`.`role_id` is not null)'
>>> App\User::find(1)->role()->get();
=> Illuminate\Database\Eloquent\Collection {#2937
     all: [],
   }
>>> App\User::find(1)->roles()->get();
=> Illuminate\Database\Eloquent\Collection {#2941
     all: [
       App\models\Role {#2930
         id: 1,
         name: "super-admin",
         display_name: "Super Admin",
         description: "This will be one permission, that can not be assigned or modified.",
         created_at: "2018-09-07 12:11:35",
         updated_at: "2018-09-07 12:11:35",
         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#2927
           user_id: 1,
           role_id: 1,
         },
       },
     ],
   }

我得到了App\User::find(1)->roles() 的结果,但我的用户模型有函数role()App\User::find(1)->role() 的集合为空,App\models\Role::find(1)->users() 的错误为

所以请给一些想法,如何解决这个问题?

【问题讨论】:

  • 如果角色给出结果,为什么不简单地将角色更改为用户模型中的角色。
  • 您的用户表中有字段“role_id”吗?
  • @CarlosSalazar 没有,但他们是另一个维护 role_user 关系的表。

标签: php laravel eloquent entrust eloquent-relationship


【解决方案1】:

我想我找到了我的问题here的答案。如果您在模型中通过 hasMany 和 belongsTo 正确定义了关系,但没有在 who belongsTo 其他表的模型表中提供外键,那么您的关系不会工作。在文档中也建议使用 foreign_key 来使用One-to-Many 关系。

Entrust 数据库设计基于多对多关系。这样用户就可以拥有多个角色。Purly 如 Laravel documentation 中所述。

【讨论】:

    【解决方案2】:

    问题在于您的关系创建方式或表格中,错误显示

     Unknown column 'users.role_id' in 'where clause'
    

    这意味着当您建立类似

    的关系时,您的用户表中缺少列 role_id
    public function users()
    {
        return $this->hasMany(User::class);
    }
    

    hasMany 将尝试在您传递的模型中找到tablename_id,如果您想通过第三张表获取它们,您可以在您的permission 模型上使用belongsToMany 或使用polymorphic relationships

    【讨论】:

    • 不,我的模型使用一对五关系。检查我的答案以获得解释。
    【解决方案3】:

    按照您定义关系的方式,您必须明确要求检索该关系:

    App\User::with('Roles')->find(1)->roles()
    

    在文档中,用户和角色的关系是这样的:

    class Role extends Model
    {
        public function users()
        {
            return $this->belongsToMany('App\User');
        }
    }
    
    class User extends Model
    {
        public function roles()
        {
            return $this->belongsToMany('App\Role');
        }
    }
    

    这样就不用问关系了

    【讨论】:

    • 那么,在User模型中定义一个方法并定义关系,RoleUserOne-To-Many关系有什么意义?,role()应该返回角色与指定用户相关联,对吧?
    • 我想要一个 hasMany 关系而不是多对多关系。
    猜你喜欢
    • 1970-01-01
    • 2013-06-04
    • 2016-11-26
    • 2018-02-19
    • 2019-07-16
    • 1970-01-01
    • 2019-06-27
    • 2014-08-03
    相关资源
    最近更新 更多