0
votes

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'patente' cannot be null (SQL: insert into cars (patente, marca, modelo, color, fecha_ingreso, updated_at, created_at) values (?, ?, ?, ?, ?, 2019-06-10 16:27:35, 2019-06-10 16:27:35)

Route::match(['get', 'post'], '/crear',[
    'uses'=>'CarController@crear',
    'as'=>'cars.crear' 
]);

Short code to form

<div class="row">

        <div class="col-md-6"></div>

            <form action="{{route('cars.crear')}}" method="post">

               @csrf

               <div class="row form-group">

                    <div class="col-md-12">

                        <label for="true">Patente:</label>

                        <input type="text" name="patente" size="6" maxlength="6" class="form-control"  required>

                    </div>

                </div>

code create and show

public function crear(Request $request){

    $patente=$request['patente'];

    $marca=$request['marca'];

    $modelo=$request['modelo'];

    $color=$request['color'];

    $fecha_ingreso=$request['fecha_ingreso'];
   
    $car=new Car();

    $car->patente=$patente;

    $car->marca=$marca;

    $car->modelo=$modelo;

    $car->color=$color;

    $car->fecha_ingreso=$fecha_ingreso;

    $car->save();
   

    return redirect()->back();

}

public function show(){

    $cars=Car::all();

    return view ('lista',['cars'=>$cars]);

}
3
Can you please provide code from your controller that actually shows the data being stored.FunkyMonk91
hello, updated code in the topic, Here is a summary of the method create public function create (Request $ request) { $ patent = $ request ['patent'];Francisco González Mejías

3 Answers

0
votes

CarController.php

public function crear(Request $request){
    request()->validate([
        'patente' => 'required',
        'marca' => 'required',
        'modelo' => 'required',
        'color' => 'required',
        'fecha_ingreso' => 'required',
        'patente' => 'required',
        'marca' => 'required',
        'modelo' => 'required',
        'color' => 'required',
        'fecha_ingreso' => 'required'
    ]);

    $car = Car::create([
        patente => $request->patente,
        marca => $request->marca,
        modelo => $request->modelo,
        color => $request->color,
        fecha_ingreso => $request->fecha_ingreso
    ]);

    return redirect()->back();
}

Your code looks fine, a bit verbose, so I cleaned it up a bit and added validation. The only other thing I can think to suggest is ensure your Car model has the fields added to the protected $fillable array.

0
votes

Just Validate Your data before store it,

php artisan make:request ClearRequest

in App\Requests\ClearRequest,

class ClearRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'patente'=>'required',
            'marca'=>'required',
            'modelo'=>'required',
            'color'=>'required',
            'fecha_ingreso'=>'required',
            'patente'=>'required',
            'marca'=>'required',
            'modelo'=>'required',
            'color'=>'required',
            'fecha_ingreso'=>'required'

        ];
    }
}

in your controller

use App\Http\Requests\ClearRequest;

in your clear method

public function crear(ClearRequest $request){
...
}

in your view file sth like this,

<form action="/clear" method="POST">
@csrf
  patente<br>
  <input type="text" name="patente">
  <br>
  marca<br>
  <input type="text" name="marca">
  <br>
  modelo<br>
  <input type="text" name="modelo">
  <br>
  fecha_ingreso<br>
  <input type="text" name="fecha_ingreso">
  <br>
  patente<br>
  <input type="text" name="patente">
  <br>

  modelo<br>
  <input type="text" name="modelo">
  <br>
  color<br>
  <input type="text" name="color">
  <br>
  fecha_ingreso<br>
  <input type="text" name="fecha_ingreso">
  <br>
  <input type="submit" value="Submit">
</form> 

if it is helpful yor you upvote me :)

-1
votes

You are trying to store a null value in a NOT NULL column. Make sure you are passing patente correctly in the request.