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

silverfox's avatar

Attaching role to user create method

Hi, I wanted some help, I wanted to assign a fixed role id '2' to new user.

my controller code for create method is

public function create() { abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

     $roles = Role::pluck('title', 'id');

    return view('users.create', compact('roles'));
}

the above method lets select role in user view. I want to simply attach the id in controller.

I tried using attach & find functions without success. Any help will be highly appreciated.

0 likes
5 replies
Snapey's avatar

save the role_id into your users table?

You would use attach if a user can have many roles, and have a role_user pivot table

silverfox's avatar

@Sergiu17 I tried this but given me undefined variable error, i will give it try again and update here

mabdullahsari's avatar

What is "without success". What is the actual error? How are your relationships defined?

Can't help if you don't even provide basic information regarding your database schemas.

Please or to participate in this conversation.