get array of roles ID of a User in laravel
<p>This is <strong>User</strong> model: </p>

<pre><code>class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function roles()
    {
        return $this-&gt;belongsToMany('App\Role');
    }
}
</code></pre>

<p>And this is <strong>Role</strong> Model: </p>

<pre><code>class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this-&gt;belongsToMany('App\User');
    }
}
</code></pre>

<p>As you can see these two models have Many to Many relationship between them. </p>

<p>Now I want to get an array of <strong>ID's</strong> of roles of a user. For that I wrote this : </p>

<pre><code>$user = User::find($id);
return $user-&gt;roles()-&gt;get(['roles.role_id']);
</code></pre>

<p>But above code returns an array containing objects refering to each role ID that user have: </p>

<pre><code>[
    {
        "role_id": 9,
        "pivot": {
            "user_id": 2,
            "role_id": 9
        }
    },
    {
        "role_id": 14,
        "pivot": {
            "user_id": 2,
            "role_id": 14
        }
    }
]
</code></pre>

<p>But I want to return an array of ID's of roles like this: </p>

<pre><code>[9,14,......]
</code></pre>

<p>How can I do that ?</p>