@warpig the problem is that you role_id is not assigned during query. Can you show your controller/service when you perform inserting?
Integrity constraint violation: Column "role_id" cannot be null
I want to be able to assign an ability to a role through the front end, but right now im encountering this issue:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'role_id' cannot be null (SQL: insert into `ability_role` (`ability_id`, `created_at`, `role_id`, `updated_at`) values (2, 2021-05-13 00:58:39, ?, 2021-05-13 00:58:39))
I have 3 tables and each of these they relate to one another, but ability_role is where I can associate an ability to a role;
- Abilities
- Roles
- Ability_role
I am using this method
public function assignAbility(Request $request, Role $role, Ability $abilities)
{
foreach ($request->abilities as $ability) {
$role->allowTo($ability);
}
return redirect()->back();
}
To receive the request sent from this form element below, I have to send the value of the ID inside of the value parameter of the <option> tag, and it can accept an array if needed.
<form
action="{{ route('assignAbility') }}"
method="POST"
> @csrf
<select
name="abilities[]"
id="abilities"
multiple
>
@foreach ($abilities as $ability)
<option value="{{ $ability->id }}">
{{ $ability->name }}
</option>
@endforeach
</select>
<button type="submit">
Assign
</button>
</form>
When I submit the form I get this message:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'role_id' cannot be null (SQL: insert into `ability_role` (`ability_id`, `created_at`, `role_id`, `updated_at`) values (2, 2021-05-13 00:58:39, ?, 2021-05-13 00:58:39))
How could I consider the role_id column on the method? Thanks.
@warpig do this
public function assignAbility(Request $request, Role $role, Ability $abilities)
{
dd($role);
}
looks like $role is new role, not existing one. Do you have it from url string as model binding?
Please or to participate in this conversation.