roles icon indicating copy to clipboard operation
roles copied to clipboard

SQLSTATE[23000]: Integrity constraint violation

Open Wipsly opened this issue 9 years ago • 1 comments

Hi,

in my usercontroller i have the following store function

public function store(Request $request)
{
    $user = new User;  
    $user->name = \Request::input('name');
    $user->email = \Request::input('email');
    $user->password = \Request::input('password');

    $user->save();  

    $role = \Request::input('role');
    $user->attachRole($role);
}

But I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (multi.role_user, CONSTRAINT role_user_role_id_foreign FOREIGN KEY (role_id) REFERENCES roles (id) ON DELETE CASCADE) (SQL: insert into role_user (created_at, role_id, updated_at, user_id) values (2015-08-11 09:35:09, admin, 2015-08-11 09:35:09, 18))

Can you help me?

Wipsly avatar Aug 11 '15 09:08 Wipsly

change it taka $user = new User; $user->name = $request->input('name'); $user->email = $request->input('email'); $user->password = $request->input('password');

        $user->save();

        $role = $request->input('role');
        $user->attachRole($role);

texnika01 avatar Aug 24 '15 08:08 texnika01