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

keung725's avatar

How can I put all roles in users list?

How can I put all roles in users list in user controller? I am using angularjs to call api.

here is my code but not work.

class UsersController extends Controller
{


    public function index()
    {
        $users = User::all->roles;
        return $users;
    }

}

e.g. UserName roles A Manger, Member B Member

0 likes
10 replies
d3xt3r's avatar
 $users = User::with('roles')->all();
keung725's avatar

BadMethodCallException in Builder.php line 2148: Call to undefined method Illuminate\Database\Query\Builder::all()

d3xt3r's avatar

Try

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

OR

    $users = User::all();
        $users->load('roles');

1 like
bobbybouwmann's avatar

@premsaurav You are not really helping by just throwing random code at the OP. You need to explain why your example works or why it would be better to do it like that.

So to answer your question, this is the best solution.

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

The reason why you got the error doing this User::with('roles')->all() is that the all method doesn't exists on the query builder. However the model does have an all method, which you can call only on the model (User::all()).

1 like
d3xt3r's avatar

@bobbybouwmann

throwing random code at the OP

Its not random, first one was just a mistake.

You need to explain why your example works or why it would be better to do it like that.

At times, it is OK to let OP debug as to what's the difference between approaches and why one won't work over other. Isn't this how one would expect to learn ?

keung725's avatar

Thanks @bobbybouwmann and @premsaurav . One thing I want to ask your opinion is:

I want to have a edit page, there will show one user details and all roles. The roles will display in html select option. If the role has not recorded in database, it will not show 'selected' in select option.

It is my user api controller

public function edit($id)
    {
        $user = User::whereId($id)->with('roles')->firstOrFail();
        
        return $user;
    }

My API just return a json include the selected roles but not show all roles in database:

{"id":1,"name":"keung","email":"keung725@hotmail.com","created_at":"2016-01-20 00:36:19","updated_at":"2016-01-19 16:36:19","roles":[{"id":1,"name":"siteowner","display_name":"Site Owner","description":"A Site Owner","created_at":"2016-01-24 13:19:42","updated_at":"2016-01-24 05:19:42","pivot":{"user_id":1,"role_id":1}},{"id":2,"name":"admin","display_name":"Admin","description":"A Site Admin","created_at":"2016-01-19 15:58:05","updated_at":"2016-01-19 15:58:05","pivot":{"user_id":1,"role_id":2}},{"id":3,"name":"member","display_name":"Member","description":"A Site Member","created_at":"2016-01-19 15:58:05","updated_at":"2016-01-19 15:58:05","pivot":{"user_id":1,"role_id":3}}]}

How can I modify this json? Thanks

keung725's avatar

I have done it, it works. See any other good ideas for me.

public function edit($id)
    {
        $user = User::whereId($id)->firstOrFail();

        $roles = array('roles' => Role::all()->toArray());

        $selectedRoles = array('selected' => $user->roles->lists('id')->toArray());

        $i = 0;
        foreach($roles['roles'] as $role) {
            if (in_array($roles['roles'][$i]['id'], $selectedRoles['selected'])) {
                $RolesArray[] = array(
                    'id' => $roles['roles'][$i]['id'],
                    'name' => $roles['roles'][$i]['name'],
                    'display_name' => $roles['roles'][$i]['display_name'],
                    'description' => $roles['roles'][$i]['description'],
                    'created_at' => $roles['roles'][$i]['created_at'],
                    'updated_at' => $roles['roles'][$i]['updated_at'],
                    'selected' => 'true'
                );
            }else{
                $RolesArray[] = array(
                    'id' => $roles['roles'][$i]['id'],
                    'name' => $roles['roles'][$i]['name'],
                    'display_name' => $roles['roles'][$i]['display_name'],
                    'description' => $roles['roles'][$i]['description'],
                    'created_at' => $roles['roles'][$i]['created_at'],
                    'updated_at' => $roles['roles'][$i]['updated_at']
                );
            }

            $i++;
        }

        $displayRolesArray =array('roles' => $RolesArray);

        $array = array_merge($user->toArray(), $displayRolesArray);
        return Response::json($array);
    }

Here is my return json.

{"id":1,"name":"keung","email":"keung725@hotmail.com","created_at":"2016-01-20 00:36:19","updated_at":"2016-01-19 16:36:19","roles":[{"id":1,"name":"siteowner","display_name":"Site Owner","description":"A Site Owner","created_at":"2016-01-24 13:19:42","updated_at":"2016-01-24 05:19:42","selected":"true"},{"id":2,"name":"admin","display_name":"Admin","description":"A Site Admin","created_at":"2016-01-19 15:58:05","updated_at":"2016-01-19 15:58:05","selected":"true"},{"id":3,"name":"member","display_name":"Member","description":"A Site Member","created_at":"2016-01-19 15:58:05","updated_at":"2016-01-19 15:58:05","selected":"true"},{"id":4,"name":"accounting","display_name":"Accounting","description":"For Accounting Department","created_at":"2016-01-24 12:00:28","updated_at":"2016-01-24 12:00:28"}]}
d3xt3r's avatar
d3xt3r
Best Answer
Level 29

You can trim it down a bit

public function edit($id)
    {
        $user = User::findOrFail($id);
        $roles = array();

        foreach(Role::all()  as $role) {

        $roleArr = [
            'id' => $role->id,
            'name' => $role->name,
            'display_name' =>$role->display_name,
            'description' => $role->description,
            'created_at' => $role->created_at
            'updated_at' => $role->updated_at
        ];
        
        // you could also simply use 
        // $roleArr  = $role->toArray() if displaying all non-hidden fields
            
        if($user->roles->contains('id',$role->id))  { // $user->roles will be a collection which has many useful methods
            $roleArr['selected'] = true;
        }
        
        $roles[] = $roleArr;
            
        }
        return Response::json(array_merge($user->toArray(),['roles'=>$roles]));
    }
1 like
keung725's avatar

@bobbybouwmann It works! Thanks a Lot. But something I don't understand. I don't know why my json is difference.

when apply my code: "updated_at":"2016-01-19 16:36:19"

when apply your code: "updated_at":{"date":"2016-01-24 12:00:28.000000","timezone_type":3,"timezone":"UTC"},"selected":true}

keung725's avatar

solved.

'created_at' => $role->created_at->toDateTimeString(), 'updated_at' => $role->updated_at->toDateTimeString()

Please or to participate in this conversation.