When I try to insert data into my database, Laravel does not insert the records, but it is strange because when I migrate the tables to be able to perform the database, Laravel creates them without any problem, I do not know what I can be doing wrong if the migration run but stored no
Route:
Route::post('/proyecto', [ctrlCars::class,'store'])->name('cars');
Controler:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\cars;
class ctrlCars extends Controller
{
public function store(Request $request){
$request->validate([
'carRegistration'=> ['required','min:6'],
'name'=> ['required','min:6'],
'fromProduction' => ['required','min:6'],
'stateStored'=> ['required'],
'model'=> ['required','min:4'],
'dateAssembled' => ['required'],
]);
$car = new cars;
$car->carRegistration = $request->carRegistration;
$car->name = $request->name;
$car->fromProduction = $request->fromProduction;
$car->stateStored = $request->stateStored;
$car->model = $request->model;
$car->dateAssembled = $request->dateAssembled;
$car-> save();
return redirect()->route('cars')->with('success','Registro guardado satisfactoriamente');
}}
Template:
@extends('header')
@section('content')
<div >
<form action="{{ route('cars') }}" method="POST">
@csrf
@if (session('success'))
<h6 >{{ session('success') }}</h6>
@endif
@error('carRegistration')
<h6 >{{ $message }}</h6>
@enderror
<p >Registro vehiculos</p>
<br>
<div >
<section >
<div >
<section >
<div >
<label for="carRegistration" >Placa</label>
<input type="text" name="carRegistration" placeholder="CDE001" maxlength="6">
</div>
<div >
<label for="name" >Nombre</label>
<input type="text" name="name" placeholder="Ferrari Enzo">
</div>
<div >
<label for="fromProduction" >Planta Produccion</label>
<input type="text" name="fromProduction" placeholder="Bmw sede1">
</div>
</section>
<section >
<div >
<label for="placa" >Fecha Ensamble</label>
<input type="date" name="dateAssembled" placeholder="CDE001">
</div>
<div >
<label for="model" >Módelo Matricula</label>
<input type="text" name="model" maxlength="4" placeholder="2013">
</div>
<div >
<label for="stateStored" >Ciudad Almacenamiento</label>
<Select type="text" id="stateStored" placeholder="Medellin">
<option value=''>Elija una opción</option>
<option value='Medellin'>Medellín</option>
<option value="Bucaramanga">Bucaramanga</option>
<option value="Cali">Cali</option>
<option value="Bogota">Bogotá</option>
</Select>
</div>
</section>
</div>
</section>
</div>
<button type="submit" >Guardar</button>
</form>
</div>
CodePudding user response:
The problem is from your template. The select tag should have a name attribute. Change your template to this
$car->dateAssembled = $request->dateAssembled;
$car-> save();
return redirect()->route('cars')->with('success','Registro guardado satisfactoriamente');
}}
Template:
@extends('header')
@section('content')
<div >
<form action="{{ route('cars') }}" method="POST">
@csrf
@if (session('success'))
<h6 >{{ session('success') }}</h6>
@endif
@error('carRegistration')
<h6 >{{ $message }}</h6>
@enderror
<p >Registro vehiculos</p>
<br>
<div >
<section >
<div >
<section >
<div >
<label for="carRegistration" >Placa</label>
<input type="text" name="carRegistration" placeholder="CDE001" maxlength="6">
</div>
<div >
<label for="name" >Nombre</label>
<input type="text" name="name" placeholder="Ferrari Enzo">
</div>
<div >
<label for="fromProduction" >Planta Produccion</label>
<input type="text" name="fromProduction" placeholder="Bmw sede1">
</div>
</section>
<section >
<div >
<label for="placa" >Fecha Ensamble</label>
<input type="date" name="dateAssembled" placeholder="CDE001">
</div>
<div >
<label for="model" >Módelo Matricula</label>
<input type="text" name="model" maxlength="4" placeholder="2013">
</div>
<div >
<label for="stateStored" >Ciudad Almacenamiento</label>
<Select type="text" name="stateStored" id="stateStored" placeholder="Medellin">
<option value=''>Elija una opción</option>
<option value='Medellin'>Medellín</option>
<option value="Bucaramanga">Bucaramanga</option>
<option value="Cali">Cali</option>
<option value="Bogota">Bogotá</option>
</Select>
</div>
</section>
</div>
</section>
</div>
<button type="submit" >Guardar</button>
</form>
</div>
CodePudding user response:
Thanks for help, the error come from because I call from name on the controller and in this input only have ID, and in the validation for this field have 'required'; I don't know how reply comments, but thanks aynber and Daniel L, you were nice help
CodePudding user response:
First time Comment your validation section and try again. If your data successfully inserted. Then Your need to modify your required field like below.
Replace ['required','min:6']
Like this: 'required|min:6',