Register user with Laravel

Asked

Viewed 945 times

-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.

3 answers

0

I had a recent problem regarding this error where both Model, Controller and Migrations were ok. After the unsuccessful attempts, I decided to analyze the code again. I realized that in the registration form I had not given the inputs a name. After inserting them, the problem was solved.

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

0


The problem with your code is using the Funcionario::create. This method uses Mass Assingment that "blocks" fields and only allows the insertion of fields that are specified in $fillable 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');
    }
  • I didn’t quite understand your answer, perhaps due to lack of knowledge, I’m still a beginner in Laravel. But from what I understand, using Funcio::create, the $fillable model must be specified users_id, right? If so, I have specified. Now about your solution, how do I assign the values to the object?

  • I changed the answer so you can see how it’s done.

  • I get it. Thank you!

-2

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

In the details it seems that Voce is passing the null value in user_id:

'users_id field does not have a default value' Field 'users_id' doesn’t have a default value

You have this users_id field in your employee table?

  • the answer part must be objective and seek to solve the question asked. These questions you have made should be made in the comment area that you will have privilege when you reach reputation 50. Visit help center for more information.

  • Thank you @Felipej.R.Vieira

Browser other questions tagged

You are not signed in. Login or sign up in order to post.