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:
as intended by the question data.
References:
Why not validate ??? if you have not implemented the Requestform class?
– novic
I don’t know how to validate that ; (
– Dan Even
Do you at least have a View ? controller so the answer is based on this?
– novic
the controller has the view yes!
– Dan Even
Then post in your question both the controller and the View?
– novic