How to treat Queryexception SQLSTATE error[23000]: Integrity Constraint Violation: 1062 Duplicate;

Asked

Viewed 1,527 times

1

The id is auto increment, but the id_uc is Unique, just not to enter an identical id_uc. I want you to return to view a message.

$storm =new ListaStorm();
$storm->id = $id;
$storm->id_uc = $id_uc;
$storm->save();

error message?

@foreach($storm as $s)
{{$s->id}} 
{{$s->id_uc }}

@endforeach
  • Why not validate ??? if you have not implemented the Requestform class?

  • I don’t know how to validate that ; (

  • Do you at least have a View ? controller so the answer is based on this?

  • the controller has the view yes!

  • Then post in your question both the controller and the View?

2 answers

1


You need to configure your application so that when the request is made the validation of your model, a minimum example with validation:

The Model

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class ListaStorm extends Model
{
    protected $table = 'listastorm';
    protected $primaryKey = 'id';
    protected $fillable = ['id_uc'];
    public $timestamps = false;
}

The validation class

Two essential validations are placed in the validation class: required this means that the data is mandatory and unique with the table name parameter, it means that this data cannot repeat:

<?php namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ListaStormRequest extends FormRequest
{    
    public function authorize()
    {
        return true;
    }


    public function rules()
    {
        return ['id_uc' => 'required|unique:listastorm'];
    }
}

The Controller

<?php namespace App\Http\Controllers;

use App\Http\Requests\ListaStormRequest;
use App\ListaStorm;
use Illuminate\Http\Request;

class ListaStormController extends Controller
{
    public function index()
    {
        return view('lista', ['items' => ListaStorm::all()]);
    }
    public function create()
    {
        return view('create');
    }
    public function store(ListaStormRequest $request)
    {
        ListaStorm::create($request->only('id_uc'));
        return redirect(route('lista.index'));
    }
}

with the appropriate routes which in the case example are 3:

Route::get('lista',['as'=>'lista.index','uses'=>'ListaStormController@index']);
Route::get('lista/create',['as'=>'lista.create','uses'=>'ListaStormController@create']);
Route::post('lista/store',['as'=>'lista.store','uses'=>'ListaStormController@store']);

to your Views:

  • index.blade.php

    @extends('layouts.app')
    
    @section('content')
        <div>
            @foreach($items as $item)
              <p> {{$item->id}} . {{$item->id_uc}} </p>
            @endforeach
        </div>
    @endsection
    
  • create.blade.php

    @extends('layouts.app')
    
    @section('content')
    @if ($errors->any())
        <div class="alert alert-danger">
             <ul>
                @foreach ($errors->all() as $error)
                     <li>{{ $error }}</li>
                @endforeach
             </ul>
        </div>
    @endif
    <div>
        <form action="{{route('lista.store')}}" method="post">
            {{ csrf_field() }}
            <input type="text" id="id_uc" name="id_uc">
            <button type="submit">Salvar</button>
        </form>
    </div>
    @endsection
    

At the time of submitting the form if the data already exists it returns in View Create and displays the message:

inserir a descrição da imagem aqui

as intended by the question data.

References:

-1

I would do so

$request->validate(['id_uc' => 'unique:listastorm,id_uc']);

Browser other questions tagged

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