Rajveer Singh
Rajveer Singh

Reputation: 433

How to display date selected from blade to blade?

I have a form in my blade with 2 dates for user to select like:

<form role="form" action="{{action('AltHr\Chatbot\PreBuiltController@viewgraphByDate', [$companyID])}}" autocomplete="off" method="POST">
  {{csrf_field()}}
  <div class="form-group-attached">
    <div class="row">
      <div class="col-lg-6">
        <div class="form-group form-group-default required" >
          <label>From</label>
          <input type="date" class="form-control" name="from" required>
        </div>
      </div>
      <div class="col-lg-6">
        <div class="form-group form-group-default required" >
          <label>To</label>
          <input type="date" class="form-control" name="to" required>
        </div>
      </div>
    </div>
  </div>
  <br/>
  <button class="btn alt-btn-black btn-sm alt-btn pull-right" type="submit">Filter Date</button>
</form>

So in my controller function I have did this:

$dateFrom = [$request->from];
$dateTo = [$request->to];

Then I tried to dd(); the value of datefrom and dateto and I am able to show it, but now I want to know how can I display this 2 values back in my blade file?

In my return view I have compact() the 2 values also.

Upvotes: 1

Views: 2292

Answers (3)

johnguild
johnguild

Reputation: 435

Firstly use Carbon in your Controller

   use Carbon\Carbon;

then do this in your blade file

   <input type="date" class="form-control" name="to" value="{{\Carbon\Carbon::parse($dateFrom)}}" required>

you can also format the date if you like to

   <input type="date" class="form-control" name="to" value="{{\Carbon\Carbon::parse($dateFrom)->format('Y-m-d))}}" required>

Upvotes: 1

jeremykenedy
jeremykenedy

Reputation: 4285

Try and update your output method from the comment from the post above to:

...

$dateFrom = $request->from;
$dateTo = $request->to;

$data = [
    'companyID' => $companyID,
    'match' => $match,
    'noAnswer' => $noAnswer,
    'missing' => $missing,
    'email' => $email,
    'pdf' => $pdf,
    'image' => $image,
    'video' => $video,
    'text' => $text,
    'totalMedia' => $totalMedia,
    'dateFrom' => $dateFrom,
    'dateTo' => $dateTo
];

return view('AltHr.Chatbot.viewgraph', $data);

...

Since you stated this is live loading the request to the page you may want to add $request->flash(); to top beginning of the receiving method.

Upvotes: 0

Kuldeep Mishra
Kuldeep Mishra

Reputation: 4050

you can use laravel form model binding to show selected values in laravel blade.

or

<input type="date" class="form-control" name="to" value="{{$to}}" required>

here $to is your returning date from controller to view in compact

Upvotes: 1

Related Questions