Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

henninghorn's avatar

Eloquent: Get relations' id (belongsToMany)

I'm trying to get an array of the associated role ids to a user.

return $this->belongsToMany('App\Role')->select(['roles.id']);

The above almost gives me what I need. But I'm also getting the pivot table. And in the end, I would like to get the ids as an array, and not id: 1, id: 2 etc. How do I do this?

"role_ids": [
    {
        "id": 1,
        "pivot": {
            "user_id": 1,
            "role_id": 1
        }
    },
    {
        "id": 2,
        "pivot": {
            "user_id": 1,
            "role_id": 2
        }
    }
]
0 likes
8 replies
JarekTkaczyk's avatar
$model->belongsToManyRelation()->getRelatedIds();
// or if you want to ensure unique ids:
$model->belongsToManyRelation()->distinct()->getRelatedIds();
henninghorn's avatar

Thanks @JarekTkaczyk

I tried your solution.

public function role_ids() 
{
    return $this->belongsToMany('App\Role')->getRelatedIds();
    return $this->belongsToMany('App\Role')->distinct()->getRelatedIds();
        return $this->roles()->getRelatedIds(); 
        return $this->roles()->distinct()->getRelatedIds(); 
}

All of the above ways to do it ends with:

Fatal error: Call to a member function addEagerConstraints() on array in /home/vagrant/Code/Acme/api/vendor/illuminate/database/Eloquent/Builder.php on line 424
JarekTkaczyk's avatar

@henninghorn You're doing it wrong. Relation method must return Relation object, not an array.

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


// then either:
$user->roles()->getRelatedIds();

// or add a method for this:
public function getRoleIds()
{
    return $this->roles()->getRelatedIds();
}

$user->getRoleIds();

And if you want to use $user->roleIds as property, then use accessor.

henninghorn's avatar

@JarekTkaczyk

I see. Thank you for your input.

I'm using l5-repository (https://github.com/andersao/l5-repository). From my controller I call:

$this->repo->with('role_ids')->find($id);

I chose to solve the problem using Fractal, which means I added a presenter that strips out the unnecessary data from the pivot table and it also puts the ids in an array.

I'm not 100 % sure that this is best practice.

JarekTkaczyk's avatar

@henninghorn Yeah, presenter is the way to prepare your response.

However, I don't know the package, so I won't suggest anything about fetching this data.

From the eloquent point of view, you could simply create an accessor that gets role ids as an array and append it to the array/json representation of your model.

henninghorn's avatar

@JarekTkaczyk

Yeah, that could be an option too. The only problems is, that I don't want to return/display role ids everytime I request the user data. I'm building an API, and only when an admin wants to edit a user the role ids should be included.

the94air's avatar

Changed in Laravel 5.4 from getRelatedIds() to allRelatedIds()

2 likes
redviking's avatar

@henninghorn Can't you just check for admin permissions in your presenter and add the array with ids instead of removing them like you do know?

Please or to participate in this conversation.