From the docs:
"A Note On Nested Attributes
If your HTTP request contains 'nested' parameters, you may specify them in your validation rules using 'dot' syntax".
Applying the unique:table_name rule on a nested parameter throws a \Illuminate\Database\QueryException.
dd($request->all());
Received parameters:
array:3 [
"role" => array:2 [
"name" => "my role"
"active" => 1
]
"permissions" => array:1 [
0 => array:4 [
"module" => "my module"
"view" => 1
"edit" => 1
"delete" => 1
]
]
"token" => "random hash"
]
Validating the name attribute:
$request->validate(['role.name' => 'required|unique:roles']);
Exception:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'role.name' in 'where clause' (SQL: select count(*) as aggregate from `roles` where `role`.`name` = my role)
The dot composition 'role.name' in this case is not parsed accordingly and passed entirely as one sql query parameter which mysql interpret as the column name 'role.name'.
From the documentation:
If the column option is not specified, the field name will be used.
In your case, this is role.name. Specify a custom column name:
$request->validate(['role.name' => 'required|unique:roles,name']);
Thanks for helping @staudenmeir.
@staudenmeir Thank you! I missed that 馃槄 I clearly need to review the docs 馃憤
Most helpful comment
From the documentation:
In your case, this is
role.name. Specify a custom column name: