I get an error when i try to insert a role
.I have found some solution of this issue in StackOverFlow but all those does not solve my problem.
I am trying to get id
from roles
table into roleID
column of user_roles
table.Here i am using Query Builder of Laravel 5.2.
public function store(Request $request)
{
//
$role = [];
$role['role'] = $request->input('role');
$data= Role::create($role);
$id= $data->id;
DB::table('user_roles')->insert([
'roleID' => $id
]);
//return $data;
return redirect(route('allRole'));
}
When i insert any role then it insert new data in roles
table but i am not getting roleID
in user_roles
table.I get an error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`amarjobs`.`user_roles`, CONSTRAINT `fkuserRolesuserID` FOREIGN KEY (`userID`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into `user_roles` (`roleID`) values (14))
Where is the problem i have done? Thanks in advanced. Searched result: