【问题标题】:Laravel Eloquent models ManyToMany get all users with their roleLaravel Eloquent 模型 ManyToMany 让所有用户获得他们的角色
【发布时间】:2017-01-13 09:21:47
【问题描述】:

亲爱的人们。

最近我开始更多地使用 Laravel 模型和 eloquent 关系,而不是编写原始查询。

现在我遇到了一个问题:

我在用户模型中得到了关系:

public function roles()
{
    return $this->belongsToMany('App\Role', 'users_has_roles', 'Users_id', 'Roles_role_id');
}

角色模型中的另一个关系:

    public function users()
{
    return $this->belongsToMany('App\User', 'users_has_roles', 'Roles_role_id', 'Users_id');
}

我可以通过在控制器中使用代码来检索所有用户并回显他们:

$users = User::with('roles')->get();
foreach ($users as $user) {
echo $user."<br>";
}

{"id":18,"name":"Ajx6bK","surname":"AwkMTWzgIB","username":"vt_2528","created_at":"2017-01-12","updated_at": null,"roles":[{"role_id":3,"role_name":"Reporter","role_description":"能够管理系统中的报告。","pivot":{"Users_id":18,"Roles_role_id ":3}}]} {"id":19,"name":"NJYMCU","surname":"ZGzjvpDAiP","username":"vt_6443","created_at":"2017-01-12","updated_at":null," roles":[{"role_id":1,"role_name":"Admin","role_description":"拥有系统中的大部分用户权限,包括用户管理。","pivot":{"Users_id":19, "Roles_role_id":1}}]} {"id":20,"name":"hVUrMG","surname":"fc72G7Ksw2","username":"vt_6610","created_at":"2017-01-12","updated_at":null," roles":[{"role_id":2,"role_name":"Data-entry","role_description":"能够管理系统中的记录。","pivot":{"Users_id":20,"Roles_role_id" :2}}]}

问题: 我无法访问表“角色”的任何字段变量。 $user->roles->role_name 或 role_description。

【问题讨论】:

    标签: php oop laravel-5 eloquent many-to-many


    【解决方案1】:

    $user-&gt;roles 是一个角色数组,因此您应该相应地访问它们:

    foreach ($user->roles as $role) {
        echo $role->role_name;
    }
    

    【讨论】:

    • 这段代码给了我错误:$users = User::with('roles')->get(); foreach ($users->role as $role) { echo $role->role_name; HomeController.php 第 38 行中的 ErrorException:未定义属性:Illuminate\Database\Eloquent\Collection::$role
    • 现在删除:未定义属性:Illuminate\Database\Eloquent\Collection::$roles
    • 只要把你有的新代码输入问题..肯定有问题
    【解决方案2】:

    roles 是对象数组,你需要循环它

    foreach($user->roles as $role){
       echo $role->role_name."<br/>";
    }
    

    还可以了解如何设置和获取属性值:http://php.net/manual/en/sdo.sample.getset.php

    【讨论】:

      【解决方案3】:

      您正在对集合进行回显,以便将其转换为 JSON。
      但是,如果您将您的集合转换为数组并使用任何转储方法(dd 或转储),您会发现现在很容易循环通过该数组来访问角色。

      $users = User::with('roles')->get();
      dd($users->toArray());

      这会输出这个

      array:4 [▼
          0 => array:8 [▶]
          1 => array:8 [▶]
          2 => array:8 [▶]
          3 => array:8 [▼
              "id" => 4
              "username" => "testcase1"
              "email" => "test1@test.com"
              "firstname" => "Test"
              "lastname" => "One"
              "created_at" => "2018-04-04 05:17:13"
              "updated_at" => "2018-04-04 05:17:13"
              "roles" => array:1 [▼
                  0 => array:5 [▼
                      "id" => 4
                      "name" => "user"
                      "created_at" => "2018-04-04 05:17:13"
                      "updated_at" => "2018-04-04 05:17:13"
                      "pivot" => array:2 [▼
                          "user_id" => 4
                          "role_id" => 4
                      ]
                  ]
              ]
          ]
      ]
      

      并且要访问角色名称和描述,您可以像这样对数组进行双重 for-each 循环

      $users = User::with('roles')->get();
      foreach ($users->toArray() as $key => $value) {
          foreach ($value['roles'] as $innerkey => $innervalue) {
              echo $innervalue['role_id'];//for role id
              echo $innervalue['role_name'];//for role name
              echo $innervalue['role_description'];//for role description
          }
      }
      exit;
      

      【讨论】:

        猜你喜欢
        • 2017-07-04
        • 2015-08-24
        • 2017-02-06
        • 2021-03-20
        • 2023-03-21
        • 2014-04-24
        • 2014-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多