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

spacedog4's avatar

Unexpected error with sync method in test environment

Laravel 5.8.34

we have 3 tables, roles, permissions and the pivot table role_permission that does not have an ID

this line of code works well in the browser

// Role.php
$this->permissions()->sync($permission);

but in my test environment I get this error where he tries to put the permissions fields in my pivot table

Unknown column 'id' in 'field list' (SQL: insert into `role_permission` (`created_at`, `id`, `label`, `name`, `permission_id`, `role_id`, `updated_at`) values (?, 2, ?, cadastrar_pessoa, 0, 1, ?))

id, label and name are from the permissions table

My test database is mysql as well my dev

A way around

I can solve that error in test doing this

// Role.php

$permission = Permission::select('id as permission_id')->whereIn('name', $permission)->get();

return $this->permissions()->sync($permission);

but I don't want to do this in all my tests

0 likes
0 replies

Please or to participate in this conversation.