M
M
Maxim Zahlupa2015-09-28 18:00:33
MySQL
Maxim Zahlupa, 2015-09-28 18:00:33

How to add roles (Role) separated by commas in a separate field, to the list of users (User) in Laravel 4, using model methods?

//User model

$users = DB::table($this->table)
            ->join('role_users', 'users.user_id', '=', 'role_users.user_id')
            ->join('roles', 'role_users.role_id', '=', 'roles.id')
            ->groupBy(['users.email'])
            ->select('users.user_id as user_id', 'users.email as email')
            ->addSelect(DB::Raw("GROUP_CONCAT(roles.name SEPARATOR ', ') as `roles`"))
            ->paginate(15);
        return $users;

this code is working, but it seems to me that it can be made more beautiful
ps I use cartalyst sentinel

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question