Compare dates in Laravel

1

I have a table called Input, which has these parameters:

public function up(){
        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();
        });
    }

I want to make a form to create an entry, but with the rule that the date for which it is reserved can not be earlier than the current one.

@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>
</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

web.php:

Route::post('reserva','EntradaController@confirmar');

And here where the reservation will be validated:

public function confirmar(Request $request){
        $normas=[
            'fecha' => 'required',
        ];

        $this->validate($request,$normas);

        if(Auth::user()->saldo<7)
            return back()->with('message', ['msg', __('No tienes suficiente dinero')]);
            else{
                $hoy=new DateTime("now");

                if($request->fecha>=$hoy){
                    $request->merge(['persona' => auth()->id()]);
                    Entrada::create($request->all());
                    Auth::user()->saldo-=7;
                    return back()->with('message', ['success', __('Reserva realizada con exito')]);
                }
                else
                    return back()->with('message', ['msg', __('No puede reservar para una fecha pasada')]);
            }
        }
    }

And to my surprise, regardless of whether the date chosen is greater or less than the current one, I always get the error message because the date chosen is earlier than the current one. Now check with dd () the dates variables and although the current date gives more information (hours, minutes, etc) than the date of the form, both dates are. Should I change the format of the dates?

Edit: I've already made the comparison, but I run into this error message:

I have verified that it happens right in the Input :: create (). What will it fail now?

    
asked by Miguel Alparez 18.02.2018 в 12:50
source

1 answer

3

Laravel has a helper that delivers the current date and time, it's now() , plus Laravel uses Carbon, a very powerful and handy DateTime wrapper.

Perhaps the simplest thing you can do is to format the value of now () so that it delivers only the date, which should result in the same format as the field type date of html (yyyy-mm-dd) ):

if ($request->fecha >= now()->toDateString()) ...

By the way, and more as a suggestion, in the controller method you have a lot of validation logic, which would be better placed in a separate form validation file: link

Finally, and more for UX, if you know in advance that you will not allow the user to book because they have less than "7" in their balance, it is better to notify the user before they even reach the form.

    
answered by 18.02.2018 / 14:38
source