Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ifrit's avatar
Level 2

How to get my range date to show up

The problem I'm having is that after I've selected a date range and the page refreshes and the date that I selected is not showing. I'm using livewire

Here is my code

<div id="reportrange" wire:model="rangeDate" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc;  width: 100%; height: 36px;  border-radius: 5px; margin-top: 4px; overflow: hidden;">
    <i class="fa fa-calendar"></i>&nbsp;
    <span wire:model="rangeDate"></span> <i class="fa fa-caret-down"></i>

    <input type="hidden"  id="startDate" wire:model="startDate">
    <input type="hidden"  id="endDate" wire:model="endDate">
</div>

<script type="text/javascript">
    $(function() {
        var start = moment();
        var end = moment();

        function cb(start, end) {
            $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
        }

        $('#reportrange').daterangepicker({
            startDate: start,
            endDate: end,
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }
        }, function(start, end){
            $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

            @this.set('startDate', start.format('YYYY-MM-DD'));
            @this.set('endDate', end.format('YYYY-MM-DD'));
        }, cb);

        cb(start, end);
    });
</script>

and this is my Index.php

<?php

namespace App\Http\Livewire\Products;

use App\Models\Product;
use Livewire\Component;

class Index extends Component
{
    public $startDate = '';
    public $endDate = '';
    public $rangeDate;

    public function render()
    {
        $products = Product::whereBetween('created_at', [$this->startDate, $this->endDate]);

        $this->rangeDate = $this->startDate;
        return view('livewire.products.index', ['products' => $products]);
    }
}
0 likes
3 replies
CorvS's avatar
CorvS
Best Answer
Level 27

@ifrit You are using wire:model on a span element, that's not supported.

You can add wire:model to any element that dispatches an input event. Even custom elements, or third-party JavaScript libraries.

If you would like to display your date, simply use the variable:

...
<span>{{ $rangeDate }}</span> <i class="fa fa-caret-down"></i>
...
1 like
guybrush_threepwood's avatar

You're also setting the startDate and endDate values manually:

    @this.set('startDate', start.format('YYYY-MM-DD'));
    @this.set('endDate', end.format('YYYY-MM-DD'));

Even though you are using data binding on your hidden inputs:

    <input type="hidden"  id="startDate" wire:model="startDate">
    <input type="hidden"  id="endDate" wire:model="endDate">

Choose one approach or the other.

Ifrit's avatar
Level 2

@corvs - thank you. I set your answer as correct since that was my question. @guybrush_threepwood - thank you for your advice I decided to remove the hidden inputs since I didn't need them because fo the @this.set()

1 like

Please or to participate in this conversation.