简体   繁体   中英

Data range between 2 dates Laravel

I need get all the values between 2 dates ( Datapicker). The user chooose dates that he want , then will show all information , but i dunno how to do that , i try it but it doesnt work :(

The first part is my controller

The second one is my searchfunction

In the final part i added an image about my final frontend

<?php

namespace App\Http\Controllers;

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

class Reportecontroller extends Controller
{
    public function index(Request $request)
    {


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

         $fechainicial = $request->input('fec_ini_instalacion');
         $fechafin = $request->input('fec_fin_instalacion');

         $Reportes= DB::table('cliente') 
         ->select('vin')
         ->whereBetween('vin',array($fechainicial,$fechafin))
         ->get();

        return view('Vistas.Reportes.index',["Reportes"=>$Reportes,"searchText"=>$query]);
        }



        }


<!---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">

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

    </span>
</div>

</div>


{{Form::close()}}

报告页面的图像

It's probably the data formatting. To fix that you can create a function and call it to convert the dates from Request:

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;
    }
}

And then, call it with the dates as parameter:

$fechainicial = $this->convertDate($request->input('fec_ini_instalacion'));
$fechafin = $this->convertDate($request->input('fec_fin_instalacion'));

You may also want to add the field vin to your cliente model's $date variable. This converts the field to a Carbon object, which can help.

protected $dates = ['vin'];

Since this is will be a Carbon instance you should convert the dates being compared to Carbon instances as well. This can be combined with Laerte's answer above. Instead of using \\DateTime::createFromFormat() you just return a new Carbon instance.

return new Carbon($date)

In the past I've had issues with Laravel and comparing dates, I've found that using the built in Carbon package and converting everything to Carbon instances to be the most helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM