Search code examples
phpformstwitter-bootstraplaravelbootstrap-datepicker

Show date field value after form submission


I am using Bootstrap date input to select date in my form. I would like to keep the date after submitting the form. It's been a few days since I started researching, but so far haven't found a solution. In my project, I'm using Laravel 5.4 and for dates, I'm using Carbon. Laravel's

    {{ old('input_date') }}

just doesn't work. If anyone could help me find a solution, it would be much appreciated.

This is my controller code:

public function showMisHistory () {
    $mises = '';
    return view('uploads.mis_history', [
        'mises' => $mises,
    ]);
}
public function showMisHistoryBetweenDates (Request $request) {
    $this->validate($request, [
        'start_date' => 'required',
        'end_date' => 'required',
    ]);
    $mises = Mis::where('uploader_pin', Auth::user()->pin)->whereBetween('date_of_data_type', array($request->start_date, $request->end_date))->get();
    // $mises = Mis::where('uploader_pin', Auth::user()->pin)->get();
    $totalLoanAmount = collect($mises)->sum('principal_loan_amount');
    $totalPremium = collect($mises)->sum('premium');
    $row_number = 1;
    return view('uploads.mis_history', [
        'mises' => $mises,
        'totalLoanAmount' => $totalLoanAmount,
        'totalPremium' => $totalPremium,
        'row_number' => $row_number,
    ]);
}

And this is form I am using.

<form action="{{ route('misHistoryBetweenDates') }}" method="POST" class="date-search-form">
                {{ csrf_field() }}
                <div class="col-md-5">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">From</label>
                        <div class="col-sm-10">
                            <input type="date" name="start_date" class="form-control" value="{{ old('start_date') }}">
                        </div>
                        @if (old('start_date'))
                        <label class="col-sm-2 control-label"></label>
                        <div class="col-sm-10">
                            <span id="helpBlock" class="help-block">{{ old('start_date') }}</span>
                        </div>
                        @endif
                    </div>
                </div>
                <div class="col-md-5">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">To</label>
                        <div class="col-sm-10">
                            <input type="date" name="end_date" class="form-control" value="{{ old('end_date') }}">
                        </div>
                        @if (old('end_date'))
                        <label class="col-sm-2 control-label"></label>
                        <div class="col-sm-10">
                            <span id="helpBlock" class="help-block">{{ old('end_date') }}</span>
                        </div>
                        @endif
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="form-group">
                        <input type="submit" name="submit" class="btn btn-primary">
                    </div>
                </div>
            </form>

Solution

  • I have solved the problem by sending the start_date and end_date separately. Here is what I did:

    public function showMisHistoryBetweenDates (Request $request) {
    $this->validate($request, [
        'start_date' => 'required',
        'end_date' => 'required',
    ]);
    $start_date = $request->start_date;
    $end_date = $request->end_date;
    $mises = Mis::where('uploader_pin', Auth::user()->pin)->whereBetween('date_of_data_type', array($request->start_date, $request->end_date))->get();
    $totalLoanAmount = collect($mises)->sum('principal_loan_amount');
    $totalPremium = collect($mises)->sum('premium');
    $row_number = 1;
    return view('uploads.mis_history', [
        'mises' => $mises,
        'totalLoanAmount' => $totalLoanAmount,
        'totalPremium' => $totalPremium,
        'row_number' => $row_number,
        'start_date' => $start_date,
        'end_date' => $end_date,
    ]);
    

    }

    And in the view:

    <form action="{{ route('misHistoryBetweenDates') }}" method="POST" class="date-search-form">
                    {{ csrf_field() }}
                    <div class="col-md-5">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">From</label>
                            <div class="col-sm-10">
                                <input type="date" name="start_date" class="form-control" value="{{ $start_date }}">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-5">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">To</label>
                            <div class="col-sm-10">
                                <input type="date" name="end_date" class="form-control" value="{{ $end_date }}">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <input type="submit" name="submit" class="btn btn-primary">
                        </div>
                    </div>
                </form>
    

    This is not what I was looking for, but does solve the problem for now.