To begin with, I have this migration:
Schema::create('animals', function (Blueprint $table){
$table->increments('id');
$table->string('nombre');
$table->string('cientifico');
$table->string('tipo');
$table->boolean('peligro');
$table->timestamps();
});
And to insert data in the table I have the following form:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Introduce los datos del animal</div>
<div class="card-body">
<form method="POST" action="animal_crear">
@csrf
<div class="form-group row">
<label for="nombre" class="col-md-4 col-form-label text-md-right">Nombre comun</label>
<div class="col-md-6">
<input id="nombre" type="text" class="form-control{{ $errors->has('nombre') ? ' is-invalid' : '' }}" name="nombre" value="{{ old('nombre') }}" required autofocus>
@if ($errors->has('nombre'))
<span class="invalid-feedback">
<strong>{{ $errors->first('nombre') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="cientifico" class="col-md-4 col-form-label text-md-right">Nombre cientifico</label>
<div class="col-md-6">
<input id="cientifico" type="text" class="form-control{{ $errors->has('cientifico') ? ' is-invalid' : '' }}" name="cientifico" value="{{ old('cientifico') }}" required autofocus>
@if ($errors->has('cientifico'))
<span class="invalid-feedback">
<strong>{{ $errors->first('cientifico') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="tipo" class="col-md-4 col-form-label text-md-right">Tipo de animal</label>
<div class="col-md-6">
<input id="tipo" type="text" class="form-control{{ $errors->has('tipo') ? ' is-invalid' : '' }}" name="tipo" value="{{ old('tipo') }}" required autofocus>
@if ($errors->has('tipo'))
<span class="invalid-feedback">
<strong>{{ $errors->first('tipo') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="peligro" class="col-md-4 col-form-label text-md-right">¿Esta en peligro de extinción?</label>
<div class="col-md-6">
Si <input type="radio" name="peligro" value=1><br>
No <input type="radio" name="peligro" value=0 checked>
@if ($errors->has('peligro'))
<span class="invalid-feedback">
<strong>{{ $errors->first('peligro') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Añadir animal
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Which is treated in this function:
use \App\Http\Requests\AnimalRequest;
public function sumar(AnimalRequest $request){
Animal::create($request->all());
return back()->with('message',['success','¡Los datos del '.$request->cientifico.' se han introducido con exito!']);
}
And I use the AnimalRequest validator:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class AnimalRequest extends FormRequest{
public function authorize(){
return true;
}
public function messages(){
'nombre.required'=>'El nombre comun es obligatorio',
'cientifico.required'=>'El nombre cientifico es obligatorio',
'tipo.required'=>'El tipo de animal es obligatorio'
}
public function rules(){
return [
'nombre'=>'required',
'cientifico'=>'required',
'tipo'=>'required'
];
}
}
But when I run the program, I find this:
What am I forgetting?
By the way, I have tried to use a normal Request and this one has worked, so the error has to be that I have not written the request correctly or that the Controller can not find AnimalRequest.