Db schema
users
-id
-name
-email
...
roles
-id
-name
User may have multiple roles and vice-versa (i already defined relationships in models)
pivot table
role_user
-id
-user_id
-role_id
Validation rule trying to make: user_id must exist in users table and has role id = 4
//...
'user_id' => ['nullable', Rule::exists('users')->where(
function ($query) {
$query->whereHas('roles',
function ($q) {
$q->where('id', 4);
}
);
}
)],
//...
Error message : "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'has' in 'where clause' (SQL: select count(*) as aggregate from
users
whereuser_id
= 0 and (has
= roles))"