Register user with Laravel

-2

I am putting together an application, where an employee is registered, and this employee belongs to a company, which comes from the users table.

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 parsing the code, and debugging it, the variable that passes the id to the officials table is populated with the id of the logged-in user. 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 give a var_dump in the variable, I have 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 can not identify the error. I have already tried to set the direct value in the body of the method, as I did with profile_id, but it does not work.

    
asked by anonymous 03.07.2018 / 20:55

3 answers

0

The problem with your code is with Funcionario::create . This method uses Mass Assingment that "blocks" fields and only allows insertion of the fields that are specified in the $fillable of the model. To solve your problem, assign the values of your request to an Employee object and use the save method.

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;

        $funcionario = new Funcionario;
        $funcionario->name = $request->name;
        $funcionario->email = $request->email;
        $funcionario->password = bcrypt($request->input('password'));
        $funcionario->perfil_id = 3;
        $funcionario->users_id = $id_empresa;

        $funcionario->save();

        return redirect('/dashboard');
    }
    
03.07.2018 / 21:26
0

I had a recent problem with this error where both Model, Controller and Migrations were ok. After unsuccessful attempts, I decided to parse the code again. I noticed that in the registration form I had not given inputs a name. After inserting them, the problem has been resolved.

Ex: < input name="title_intro" type="text" placeholder="insert title" >

    
14.01.2019 / 05:53
-2

Good afternoon, in this case would not be perfil_id in place of User_id?

In the details it seems to me that you are passing the null value in user_id:

'field users_id does not have a default value' Field 'users_id' does not have a default value

Do you have this field users_id in your employee table?

    
03.07.2018 / 21:12