I have a User table and an Entry table, which is for a user to buy an entry to visit a botanical garden.
Ticket data:
Schema::create('entradas', function (Blueprint $table){
$table->increments('id');
$table->date('fecha');
$table->unsignedInteger('persona'); // Quien ha comprado la entrada.
$table->foreign('persona')->references('id')->on('users');
$table->timestamps();
});
We must also bear in mind that the User
table will have a variable 'balance', which will indicate how much money that person has left. Buying a ticket costs € 7.
Form to buy the ticket:
@extends('layouts.app')
@section('content')
@Logged()
@include('partials.errors')
<div align="center" class="panel panel-default">
<h1 class="text-center text-mute"> {{ __("Nueva reserva") }} </h1>
<h2>Recuerda que una entrada son 7€<br>Tienes actualmente {{Auth::user()->saldo}}€</h2>
</div>
<div class="row">
<form method="POST" action="../reserva">
{{ csrf_field() }}
<div class="form-group">
<label for="fecha" class="col-md-12 control-label"> {{ __("Indique la fecha") }}
</label>
<input id="fecha" style="width:150px" type="date" class="form-control" name="fecha" value="{{ old('fecha') }}"/>
</div>
<button type="submit" name="addPlanta" class="btn btn-default"> {{ __("Reservar entrada") }}
</button>
</form>
</div>
@else
<h1 class="text-center text-mute" style="color:#FF0000"> {{ __("Debes haber iniciado sesión para crear una reserva") }} </h1>
@endLogged
@endsection
Once filled out, we go to the following web.php site:
Route::post('reserva','EntradaController@confirmar');
And this brings us to the next function:
public function confirmar(EntradaRequest $r){
if(Auth::user()->saldo<7)
return back()->with('message', ['msg', __('No tienes suficiente dinero')]);
elseif($r->fecha>=now()->toDateString()){
$r->merge(['persona' => auth()->id()]);
Entrada::create($r->input());
Auth::user()->saldo-=7;
Auth::user()->save();
return back()->with('message', ['success', __('Reserva realizada con exito. Se han descontado 7€ de tu saldo')]);
}
return back()->with('message', ['msg', __('No puede reservar para una fecha pasada')]);
}
During this function, InputRequest.php is used, which has this code:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class EntradaRequest extends FormRequest{
public function authorize() {
return auth()->check();
}
public function rules(){
return [
'fecha' => 'required',
];
}
}
But when trying to execute the questionnaire, I run into this:
I have verified that the error is concretely in Entrada::create($r->input());