-2
I am setting up an application, where it is made registration of an employee, and this employee belongs to a company, which comes from the table users.
When I do the registration process, the following error message appears:
"SQLSTATE[HY000]: General error: 1364 Field 'users_id' doesn't have a default value (SQL: insert into `funcionarios` (`name`, `email`, `password`, `perfil_id`, `updated_at`, `created_at`) values (Gustavo, [email protected], $2y$10$nRqXsSPGybF1rMFQRsMwLOmiofkGp7voaJRnOwUEewMYXR1oyBDya, 3, 2018-07-03 01:36:22, 2018-07-03 01:36:22)) ◀"
However, by analyzing the code, and debugging it, the variable that passes the id to the table functios comes filled with the logged-in user id. The method is as follows:
public function cadastraFuncionario(Request $request){
$this->validate($request, [
'name' => ['required', 'max:255'],
'email' => ['required', 'max:255', 'email', 'unique:funcionarios'],
'password' => ['required', 'min:6', 'max:20']
]);
$id_empresa = Auth::user()->id;
$credentials = array_merge($request->all(), [
'password' => bcrypt($request->input('password')),
'perfil_id' => 3,
'users_id' => $id_empresa,
]);
var_dump($credentials);
Funcionario::create($credentials);
return redirect('/dashboard');
}
When I var_dump the variable, I get the following return:
C:\Users\Gustavo\project-manager\app\Http\Controllers\FuncionarioController.php:34:
array (size=6)
'_token' => string 'xAhxSeW6hQVb1k5PFYhgTvd8Y7rWkmn38IXt08ND' (length=40)
'name' => string 'Gustavo' (length=7)
'email' => string '[email protected]' (length=18)
'password' => string '$2y$10$hS816jGMo2BHpgL8/7d/buWmrwnKKjjiW.dIxYwnEFaMMwyAgZClS' (length=60)
'perfil_id' => int 3
'users_id' => int 1
I cannot identify the error. I have even tried to set the direct value in the method body, as I did with perfil_id, but it does not work.
Please add your Model Funcionario to the example to make it easier to understand.
– Felipe J. R. Vieira