Skip to content
Advertisement

Laravel How to set default time as today in daterange filter and how to display the data?

Hi i want to set value of my daterange filter as real-time like today is 5 April 2021 so the default of my daterange filter become from 5 April 2021 to 5 April 2021 and also display the data from my database, what should i do?

This is my View Blade:

  <form action="searchdateinvoice" method="get">
        @csrf
        <div class="container">
            <div class="row">
                <div class="container-fluid">
                    <div class="form-group row">
                        <label for="date" class="col-form-label col-sm-1" style="width: 99px">Date From</label>
                        <div class="col-sm-3">
                            <input type="date" class="form-control input-sm"  value="2021-04-05"name="dateFrom" id="dateFrom" required>
                        </div>
                        
                        <label for="date" class="col-form-label col-sm-1">Date To</label>
                        <div class="col-sm-3">
                            <input type="date" class="form-control input-sm "  value="2021-04-05"name="dateTo" id="dateTo" required>
                        </div>
                        <div class="col-sm-3">
                            <button type="submit" class="btn btn-primary pl-4 pr-4 rounded-pill" style="background-color: #B10000" title="searchdate"> <span
                                    class="pl-1">Filter</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    
 @if (count($indexInvoices) > 0)
        <table class="table table-bordered table-hover" class="display" cellspacing="0">
            <thead style="background-color: #B10000">
                <tr>
                    <th class="text-center text-light" scope="col">No.</th>
                    <th class="text-center text-light" style="width:10%" scope="col">Sale Date</th>
                    <th class="text-center text-light" scope="col">Invoice</th>
                    <th class="text-center text-light" scope="col">Sender</th>
            <th class="text-center text-light" scope="col">Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($indexInvoices as $invoice)
                        <tr>
                            <th class="align-middle" scope="row">{{ $loop->iteration }}</th>
                            <th class="align-middle" scope="row">
                                @php
                                    $old_date = explode('-', $invoice->saledate);
                                    $new_data = $old_date[2] . '-' . $old_date[1] . '-' . $old_date[0];
                                    echo $new_data;
                                @endphp
                            </th>
                            <th class="align-middle" scope="row">{{ $invoice->saleno }}</th>
                            <th class="align-middle" scope="row">{{ $invoice->chkby }}</a></th>
                            <th class="align-middle" scope="row">
                                @if ($invoice->isposted == false)
                                    @php
                                        echo 'Pending';
                                    @endphp
                                @else
                                    @php
                                        echo 'Finished';
                                    @endphp
                                @endif
                        </tr>
                    @endif
                @endforeach
            </tbody>
        </table>
        {{ $indexInvoices->links('pagination::bootstrap-4') }}

This is my Invoice Controller:

    public function searchDateInvoice(Request $request)
    {
        $fromDate = $request->get('dateFrom');
        $toDate = $request->get('dateTo');
        if(!empty($fromDate))
        {
            $searchDateInvoice = Invoices::whereBetween('saledate', [$fromDate, $toDate])
                        ->orderby('saleno', 'desc')
                        ->paginate(15);
            $searchDateInvoice->appends(['dateFrom'=>$fromDate,'dateTo'=>$toDate]);
        }
        return view('invoices.searchdateinvoice')->with(['searchDateInvoice'=>$searchDateInvoice]);
    }

This my Route (web.php)

Route::get('/invoices','AppHttpControllersInvoiceController@indexInvoice');                                                                                                                 
Route::get('/searchInvoice','AppHttpControllersInvoiceController@searchInvoice')->name('searchInvoice');   
Route::get('/searchdateinvoice','AppHttpControllersInvoiceController@searchDateInvoice')->name('searchdateinvoice');

Advertisement

Answer

use date() to get current date with any format you want.

<div class="col-sm-3">
    <input type="date" class="form-control input-sm"  value="{{date('Y-m-d')}}"name="dateFrom" id="dateFrom" required>
</div>
                        
<label for="date" class="col-form-label col-sm-1">Date To</label>
<div class="col-sm-3">
    <input type="date" class="form-control input-sm "  value="{{date('Y-m-d')}}"name="dateTo" id="dateTo" required>
</div>

As for the little transformation in the sedond part, it can use date too

@php
    echo date('d-m-Y', strtotime($invoice->saledate));
@endphp

// or just

{{date('d-m-Y', strtotime($invoice->saledate))}}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement