欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

laravel怎么取出多对多关联表的一个字段,组成一个数组? 多对多 id 数组 laravel many to many get id array Get ids array from related laravel model which is having belongsToMany relationship 有大用

I have a model Role which belongs to many Users.

Class Role {
     public $fillable = ["name"];

     public function users()
     {
          return $this->belongsToMany('App/Models/User')->select(['user_id']);
     }}

When I retrieve users using with query in Role. I want It would return only user_ids array

 Role::with("users")->get();

it should return the following output

 [ 
   {
     "name": "Role1",
     "users" : [1,2,3]
   },
   {
     "name": "Role2",
     "users" : [1,2,3]
   }
 ]

Currently it gives following output

[ 
   {
     "name": "Role1",
     "users" : [
        {
           user_id : 1
        },
        {
           user_id : 2
        },

        {
           user_id : 3
        }
   },
   {
     "name": "Role2",
     "users" : [
        {
           user_id : 1
        },
        {
           user_id : 2
        },

        {
           user_id : 3
        }
     ]
   }
 ]
shareimprove this questionedited Aug 19 '15 at 11:58asked Aug 19 '15 at 8:02sp115316


   You might be able to achieve it using ->lists('user_id') instead of ->all(). – Jeemusu Aug 19 '15 at 8:09    try this return $this->belongsToMany('App/Models/User')->lists('user_id'); – Anil Sharma Aug 19 '15 at 8:17   Sorry I have used "->get" not "->all". I have edited my question. @Jeemusu, it is giving array of ids for all records, I need user_ids per role – sp11 Aug 19 '15 at 12:04   @Creator I have tried using list but not working – sp11 Aug 19 '15 at 12:04



1 Answer 正确答案

Personally, I wouldn't change the users() relationship, but maybe add an accessor for user IDs

class Role {
    protected $fillable = ["name"];

    // adding the appends value will call the accessor in the JSON response
    protected $appends = ['user_ids'];

    public function users()
    {
         return $this->belongsToMany('App/Models/User');
    }

    public function getUserIdsAttribute()
    {
        return $this->users->pluck('user_id')->toArray();
    }}

Then you still have a working relationship, but can access the user IDs as an array in the Role response. If that doesn't work for you, as mentioned by @Creator, you could probably just add ->lists('id') in the relationship rather than the select()

shareimprove this answeredited May 24 at 5:14DokiCRO6061618answered Aug 19 '15 at 8:47benJ926611

   It's working. Thank you :) – sp11 Aug 19 '15 at 12:10

来自  https://stackoverflow.com/questions/32089782/get-ids-array-from-related-laravel-model-which-is-havin...


laravel怎么取出多对多关联表的一个字段,组成一个数组?
比如:
users表和roles表是多对多关系,关联表是这样的:

id user_id role_id

我想获取当前登录用户的所有role_id:

public function test() {     //$roles是一个集合     $roles=Auth::user()->roles;              //我想得到$roles里面的role_id,组成一个数组,像这样:     //$roleIds=[1,2,3]; }

问题见代码注释。


 编程开发
  
登录 后发表回答
1条回答
 
mishen 
2楼-- · 2017-08-20 21:38


$roles=Auth::user()->roles->map(function($role) {
    return $role->id;
})

或者直接获取中间表的role_id:


foreach (Auth::user()->roles as $role) {    echo $role->pivot->role_id;
}



来自 https://www.ask2.cn/q-314414.html




laravel怎么取出多对多关联表的一个字段,组成一个数组?
比如:
users表和roles表是多对多关系,关联表是这样的:

iduser_id
role_id

我想获取当前登录用户的所有role_id:

public function test(){    //$roles是一个集合
    $roles=Auth::user()->roles;        
    //我想得到$roles里面的role_id,组成一个数组,像这样:
    //$roleIds=[1,2,3];}

问题见代码注释。

3个回答

0
$roles=Auth::user()->roles->map(function($role) {    return $role->id;
})

或者直接获取中间表的role_id:

foreach (Auth::user()->roles as $role) {    echo $role->pivot->role_id;
}
0
$roleIds = array_pluck($roles,'id');
如果$roles是对象,就先$roles->toArray()成为数组
0

直接查询的时候后面加上->toArray()
users::pluck('id')->toArray();

撰写答案



来自  https://segmentfault.com/q/1010000010753592?sort=created

普通分类: