Skip to content
Advertisement

Can’t insert data on my database from laravel

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 AppHttpControllers;

use IlluminateHttpRequest;
use AppModelscars;

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 class="container w-10 mt-5 border p-4">

    <form action="{{ route('cars') }}" method="POST">
        @csrf

        @if (session('success'))
            <h6 class="alert alert-success">{{ session('success') }}</h6>
        @endif

        @error('carRegistration')
            <h6 class="alert alert-danger">{{  $message }}</h6>
        @enderror

        <p class="h2">Registro vehiculos</p>
        <br>
        <div class="row">
            <section class="col-md-12">
                <div class="form-group">
                    <section class="row">
                        <div class="col-md-4">
                            <label for="carRegistration" class="form-label">Placa</label>
                            <input type="text" class="form-control" name="carRegistration" placeholder="CDE001" maxlength="6">
                        </div>
                    
                        <div class="col-md-4">
                            <label for="name" class="form-label">Nombre</label>
                            <input type="text" class="form-control" name="name" placeholder="Ferrari Enzo">
                        </div>

                        <div class="col-md-4">
                            <label for="fromProduction" class="form-label">Planta Produccion</label>
                            <input type="text" class="form-control" name="fromProduction" placeholder="Bmw sede1">
                        </div>
                    </section>

                    <section class="row mt-4">
                        <div class="col-md-4">
                            <label for="placa" class="form-label">Fecha Ensamble</label>
                            <input type="date" class="form-control" name="dateAssembled" placeholder="CDE001">
                        </div>
                    
                        <div class="col-md-4">
                            <label for="model" class="form-label">Módelo Matricula</label>
                            <input type="text" class="form-control" name="model" maxlength="4" placeholder="2013">
                        </div>

                        <div class="col-md-4">
                            <label for="stateStored" class="form-label">Ciudad Almacenamiento</label>
                            <Select type="text" class="form-control" 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" class="btn btn-success mt-4">Guardar</button>
    </form>

</div>

Advertisement

Answer

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 class="container w-10 mt-5 border p-4">

    <form action="{{ route('cars') }}" method="POST">
        @csrf

        @if (session('success'))
            <h6 class="alert alert-success">{{ session('success') }}</h6>
        @endif

        @error('carRegistration')
            <h6 class="alert alert-danger">{{  $message }}</h6>
        @enderror

        <p class="h2">Registro vehiculos</p>
        <br>
        <div class="row">
            <section class="col-md-12">
                <div class="form-group">
                    <section class="row">
                        <div class="col-md-4">
                            <label for="carRegistration" class="form-label">Placa</label>
                            <input type="text" class="form-control" name="carRegistration" placeholder="CDE001" maxlength="6">
                        </div>
                    
                        <div class="col-md-4">
                            <label for="name" class="form-label">Nombre</label>
                            <input type="text" class="form-control" name="name" placeholder="Ferrari Enzo">
                        </div>

                        <div class="col-md-4">
                            <label for="fromProduction" class="form-label">Planta Produccion</label>
                            <input type="text" class="form-control" name="fromProduction" placeholder="Bmw sede1">
                        </div>
                    </section>

                    <section class="row mt-4">
                        <div class="col-md-4">
                            <label for="placa" class="form-label">Fecha Ensamble</label>
                            <input type="date" class="form-control" name="dateAssembled" placeholder="CDE001">
                        </div>
                    
                        <div class="col-md-4">
                            <label for="model" class="form-label">Módelo Matricula</label>
                            <input type="text" class="form-control" name="model" maxlength="4" placeholder="2013">
                        </div>

                        <div class="col-md-4">
                            <label for="stateStored" class="form-label">Ciudad Almacenamiento</label>
                            <Select type="text" name="stateStored" class="form-control" 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" class="btn btn-success mt-4">Guardar</button>
    </form>

</div>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement