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

tzak9022's avatar

How to pass a variable to datepicker

How to pass a variable to datepicker, I have 2 variables, $from and $to, I would like to pass those variable to the first datepicker and second one

I have the following controller:

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Gate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;

class VouchersAccountingController extends Controller
{
    public function index()
    {
        abort_if(Gate::denies('vouchers_accounting_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $from = date('2022-08-01');
        $to = date('2022-08-29');

        $Reports = DB::table('create_vouchers')
            ->select('create_vouchers.id', 'client_name', 'night', 'hotels.hotel_name', 'arrivaldate', 'departuredate', 'total_amount', 'number_of_room', 'payment_mode')
            ->join('hotels', 'hotels.id', 'create_vouchers.hotel_name_id')
            ->whereBetween('arrivaldate', [$from, $to])->get();


        return view('admin.vouchersAccountings.index', compact(['Reports']));
    }
}

My view is:

@extends('layouts.admin')
@section('content')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script>
<link href="{{ asset('css/arrivaldeparture.css') }}" rel="stylesheet" />
<div class="content">

    <div class="row">
        <div class="col-md-12 mt-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    {{ trans('cruds.vouchersAccounting.title') }}

                </div>

                <div class="panel-body">



                    <div class="card">
                        <div class="card-header">
                            <h4 class="Green">Reports</h4>

                            <script type="text/javascript">
                                $(function() {
                                    $('#datetimepicke').datetimepicker();
                                });
                            </script>

                            <div class="form-group">
                                From
                                <div class='input-group date' id='CalendarDateTime'>
                                    <input type='text' class="form-control" />
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                </div>
                            </div>

                            <div class="form-group">
                                To
                                <div class='input-group date' id='CalendarDateTime'>
                                    <input type='text' class="form-control" />
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                </div>
                            </div>
                            <button type="button" class="btn btn-primary">Search</button>
                            <div class="card-body">
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th>Voucher #</th>
                                            <th>Client</th>
                                            <th>Hotel</th>
                                            <th>Arrival</th>
                                            <th>Departure</th>
                                            <th>Nights</th>
                                            <th>Rooms</th>
                                            <th>Payment Mode</th>
                                            <th>Paid Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($Reports as $Report)
                                        <tr>
                                            <th> {{$Report->id}} </th>
                                            <th> {{$Report->client_name}} </th>
                                            <th> {{$Report->hotel_name}} </th>
                                            <th> {{$Report->arrivaldate}} </th>
                                            <th> {{$Report->departuredate}} </th>
                                            <th> {{$Report->night}} </th>
                                            <th> {{$Report->number_of_room}}</th>
                                            <th> {{$Report->payment_mode}}</th>
                                            <th> {{$Report->total_amount}}</th>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="{{ asset('js/va.js')}}"></script>
@endsection
0 likes
5 replies
ksi's avatar

Try this

// send from & to to the view
return view('admin.vouchersAccountings.index', compact('Reports', ‘from’, ‘to’));
// add id to get the FROM date input
<input id=“date-from” type='text' class="form-control" />
// same for TO date
<input id=“date-to” type='text' class="form-control" />

// in your script:
$(function () {
 $(‘#date-from’).datepicker({‘dateFormat’: ‘yyyy-mm-dd’});
 $(‘#date-from’).datepicker(‘setDate’, ‘{{ $from }}’);
 $(‘#date-to’).datepicker({‘dateFormat’: ‘yyyy-mm-dd’});
 $(‘#date-to’).datepicker(‘setDate’, ‘{{ $to }}’); 
})
tzak9022's avatar

@ksi Thank you my friend for your support I have a few questions.

should I set the $from and $to variable to be empty like this:

 $from = date('');
 $to = date('');

when I set the dates while inspecting the page, the console doesn't show anything, if you noticed I have a search button, how can I make it execute the script?

My Controller and view after modifications:

Controller:

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Gate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;

class VouchersAccountingController extends Controller
{
    public function index()
    {
        abort_if(Gate::denies('vouchers_accounting_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $from = date('');
        $to = date('');

        $Reports = DB::table('create_vouchers')
            ->select('create_vouchers.id', 'client_name', 'night', 'hotels.hotel_name', 'arrivaldate', 'departuredate', 'total_amount', 'number_of_room', 'payment_mode')
            ->join('hotels', 'hotels.id', 'create_vouchers.hotel_name_id')
            ->whereBetween('arrivaldate', [$from, $to])->get();


        //return view('admin.vouchersAccountings.index', compact(['Reports']));
        // send from & to to the view
        return view('admin.vouchersAccountings.index', compact('Reports', 'from', 'to'));
    }
}

View:

@extends('layouts.admin')
@section('content')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script>
<link href="{{ asset('css/arrivaldeparture.css') }}" rel="stylesheet" />
<div class="content">

    <div class="row">
        <div class="col-md-12 mt-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    {{ trans('cruds.vouchersAccounting.title') }}

                </div>

                <div class="panel-body">



                    <div class="card">
                        <div class="card-header">
                            <h4 class="Green">Reports</h4>

                            <script type="text/javascript">
                                $(function() {
                                    $('#datetimepicke').datetimepicker();
                                });
                            </script>

                            <script>
                                $(function() {
                                    $('#date-from').datepicker({
                                        'dateFormat': 'yyyy-mm-dd'
                                    });
                                    $('#date-from').datepicker('setDate', '{{ $from }}');
                                    $('#date-to').datepicker({
                                        'dateFormat': 'yyyy-mm-dd'
                                    });
                                    $('#date-to').datepicker('setDate', '{{ $to }}');
                                })
                            </script>

                            <div class="form-group">
                                From
                                <div class='input-group date' id='CalendarDateTime'>
                                    <input id="date-from" type='text' class="form-control" />
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                </div>
                            </div>

                            <div class="form-group">
                                To
                                <div class='input-group date' id='CalendarDateTime'>
                                    <input id="date-to" type='text' class="form-control" />
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                </div>
                            </div>
                            <button type="button" class="btn btn-primary">Search</button>
                            <div class="card-body">
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th>Voucher #</th>
                                            <th>Client</th>
                                            <th>Hotel</th>
                                            <th>Arrival</th>
                                            <th>Departure</th>
                                            <th>Nights</th>
                                            <th>Rooms</th>
                                            <th>Payment Mode</th>
                                            <th>Paid Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($Reports as $Report)
                                        <tr>
                                            <th> {{$Report->id}} </th>
                                            <th> {{$Report->client_name}} </th>
                                            <th> {{$Report->hotel_name}} </th>
                                            <th> {{$Report->arrivaldate}} </th>
                                            <th> {{$Report->departuredate}} </th>
                                            <th> {{$Report->night}} </th>
                                            <th> {{$Report->number_of_room}}</th>
                                            <th> {{$Report->payment_mode}}</th>
                                            <th> {{$Report->total_amount}}</th>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="{{ asset('js/va.js')}}"></script>
@endsection
ksi's avatar

@tzak9022 you set those variables to whatever you want them to be. Probably you want them to be populated from the request

tzak9022's avatar

@ksi yes thats what im talking about how to populate them from the request through the script?

Please or to participate in this conversation.