Hello, I am doing a crud with laravel (latest version) and postgresql. The problem is that it does not save data. The database is connected and I already did a migration.
public function store(Request $request)
{
$user = new User;
$user->nombres = $request->nombres;
$user->apellido_paterno = $request->apellido_paterno;
$user->apellido_materno = $request->apellido_materno;
$user->rut = $request->rut;
$user->telefono = $request->telefono;
$user->email = $request->email;
$user->status = $request->estado;
$user->fecha_nacimiento = $request->fnacimiento;
return redirect()->route('welcome')
->with('info', 'Usuario creado');
}
The form sends me to the main page (which is correct, but with the form data in the link and does not save anything else)
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css"></head>
<form action="{{route('store')}}" method="POST">
<div class="row">
<div class="col-6">
<form-group>
<label for="">Ingrese los nombres del usuario</label>
<input type="text" class="form-control" name="nombres" required>
</form-group>
<form-group>
<label for="">Ingrese el apellido paterno</label>
<input type="text" class="form-control" name="apellido_paterno" required>
</form-group>
<form-group>
<label for="">Ingrese el apellido materno</label>
<input type="text" class="form-control" name="apellido_materno" required>
</form-group>
<form-group>
<label for="">Ingrese el email</label>
<input type="email" class="form-control" name="email" required>
</form-group>
</div>
<div class="col-6">
<form-group>
<label for="">Ingrese el rut del usuario</label>
<input type="text" class="form-control" name="rut" required>
</form-group>
<form-group>
<label for="">Ingrese el telefono</label>
<input type="text" class="form-control" name="telefono" required>
</form-group>
<form-group>
<label for="">Fecha de nacimiento</label>
<input type="date" class="form-control" name="fnacimiento" required>
</form-group>
<form-group>
<label for="">Ingrese el estado del usuario</label>
<input type="number" class="form-control" name="estado" required>
</form-group>
</div>
<button class="btn btn-primary mt-3 ml-3" type="submit">Guardar</button>
</form>
Any input is appreciated :P
At no point are you telling it to save the model:
At the end of your code you need to tell the $user object to access the save(); so that in this way the registration is processed, remaining like this
$user->save();
That is, it must go right after this line
$user->date_birth = $request->fbirth;
Here you have the official doc to consult, go to the INSERT section
Just in case, there is a different and even simpler way in some cases to create models, the create() method, which accepts an array as a parameter: