Get data between 2 laravel dates

0

Through the 2 inputdate I want to get all the records included between them 2. I have the query built but when I hit the search button nothing happens just shows me the 2 data in the firefox bar, I would appreciate your help

My driver

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\ClienteModel;


class ReporteController extends Controller
{

private function convertDate($date)
{
    if ($date === null) {
        return null;
    }

    $dt = \DateTime::createFromFormat('d/m/Y', $date);
    if ($dt) {
        return $dt->format('Y-m-d');
    } else {
        return null;
    }
}


public function filtrar(Request $request){
 	if ($request){


        $query=trim($request->get('searchText'));

		$fec_email = $this->convertDate($request->Input('fec_email'));
		$fec_fin_instalacion = $this->convertDate($request->Input('fec_fin_instalacion'));

         $Reportes= DB::table('cliente')
         ->whereBetween('fec_ini_instalacion',[$fec_email,$fec_fin_instalacion])
         ->get();
       // return view('Vistas.Reportes.index', compact('Reportes'));
       return view('Vistas.Reportes.index',["Reportes"=>$Reportes,"searchText"=>$query]);


         }

}

}

Vista search

<!---Recordar que dicha ruta llamara a nuestro metodo index  y realizara el filtro por medio del searchtext-->

{!! Form::open(array('url'=>'Vistas/Reportes','method'=>'GET','autocomplete'=>'off','role'=>'search'))!!}

<div class="form-group">
	<label for="">Fecha email</label>
		<input type="date"  class="form-control" name="fec_email"  value="fec_email">
</div>

<div class="form-group">
	<label for="">Fin de la instalacion </label>
		<input type="date"  class="form-control" name="fec_fin_instalacion" value="fec_fin_instalacion">
</div>

<div class="form-group">
<div class="input-group">
     <span class="input-group-btn">
    	 <button type="submit" class="btn btn-primary" value="{{ $searchText }}">Buscar</button>

    </span>
</div>

</div>




{{Form::close()}}

index

@extends('template.admin')


@section('contenido')

<h1>Reportes</h1>

<div class="row">
 <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12">
      @include('Vistas.Reportes.search')
 </div>

</div>
<div class="row">
	<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
		<div class="table-responsive">
			<table class="table table-striped table-bordered table-condensed table-hover">

               <thead>
               	<th>Fecha</th>
               </thead>
               @foreach($Reportes as $repo)

               <tr>
               	 <td>{{ $repo->fec_ini_instalacion }}</td>

               </tr>

                @endforeach

			</table>

		</div>

	</div>

</div>

@endsection

    
asked by DANIEL FELIPE LOPEZ VARGAS 10.01.2018 в 20:54
source

2 answers

1

I from sql practically nothing, but I made some markers of which the icon depended on the difference of the current date with one coming from an xml, maybe it will help you

                      var date = ultvisita;
                      var newdate = date.split("-").reverse().join("-");
                      console.log(newdate);//eso fue para cambiar el formato 
                     dela fecha
                    var fecha1 = moment();//eso es la fecha de hoy en segundos
                    var fecha2 = moment(newdate);//eso la fecha convertida en segundos
                    var difdias = fecha1.diff(fecha2, 'days');//esto resta 
                            ambas y nos devuelve la cantidad de dias de 
                      diferencia,luego hice unos if y me 
                      sirvio      
                     console.log(difdias)
                      </script>

Hopefully, and if it helps you, "src=" https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js ">" (I do not know why I would go in the code when I was going to send it), maybe filtering with a variable as difdias serves you.

    
answered by 10.01.2018 в 22:15
0

As a recommendation, implement a datepicker to insert the dates. If your user allows you to enter the date manually in the text field, put a validation on your form and on your controller.

So when you enter the date in the correct format you make the appropriate conversion and search:

private function convertDate($date)
{
    if ($date === null) {
        return null;
    }

    //Preferentemente es recomendable que hagas una 
    //función que no permita que la primera fecha
    //que sea mayor a la segunda 

    $dt = date('Y-m-d H:i:s', strtotime($date)); //tal vez funcione>>>   date('Y-m-d', strtotime($date));  
    if ($dt) {
        return $dt->format('Y-m-d');
    } else {
        return null;
    }
}

I hope it works for you, but to see if we see.

    
answered by 10.01.2018 в 22:13