You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Blade视图间展示用户选中的日期?

Displaying Selected Dates in Blade After Form Submission

Got it, let's walk through how to show the dates your user picks in the target Blade view once they submit the form. Here's a step-by-step breakdown:

1. First, Complete Your Date Range Form

It looks like your form was cut off, so let's add the "To" date field and a submit button to make it functional:

<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 value="{{ old('from') }}">
                </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 value="{{ old('to') }}">
                </div>
            </div>
        </div>
        <button type="submit" class="btn btn-primary mt-3">View Graph</button>
    </div>
</form>

Note: The value="{{ old('from') }}" part lets you retain the selected date if the form is reloaded (like after validation errors) — super handy for user experience.

2. Update Your Controller to Pass Dates to the View

In your viewgraphByDate method, you'll need to grab the submitted dates, optionally format them for readability, and send them to your target view:

use Illuminate\Http\Request;
use Carbon\Carbon; // If you want to format dates (recommended)

public function viewgraphByDate(Request $request, $companyID)
{
    // Optional: Validate the dates to ensure they're valid and "To" isn't before "From"
    $request->validate([
        'from' => 'required|date',
        'to' => 'required|date|after_or_equal:from',
    ]);

    // Get the raw submitted dates (format: YYYY-MM-DD)
    $selectedFrom = $request->input('from');
    $selectedTo = $request->input('to');

    // Optional: Format dates to a friendlier format (e.g., "May 20, 2024")
    $formattedFrom = Carbon::parse($selectedFrom)->format('F j, Y');
    $formattedTo = Carbon::parse($selectedTo)->format('F j, Y');

    // Pass the dates to your target view (replace 'chatbot.graph' with your actual view path)
    return view('chatbot.graph', compact('selectedFrom', 'selectedTo', 'formattedFrom', 'formattedTo'));
}

3. Display the Dates in Your Target Blade View

Now you can show the dates however you want in the view that loads after submission. Here are a couple of common approaches:

Option 1: Show Raw Date Format (YYYY-MM-DD)

<div class="mb-4">
    <h5>Selected Date Range:</h5>
    <p>From: <strong>{{ $selectedFrom }}</strong></p>
    <p>To: <strong>{{ $selectedTo }}</strong></p>
</div>

Option 2: Show Formatted, User-Friendly Dates

<div class="mb-4">
    <h4>Graph for Period: <em>{{ $formattedFrom }} to {{ $formattedTo }}</em></h4>
</div>

That's it! Your selected dates will now be displayed in the target view after the user submits the form.

内容的提问来源于stack exchange,提问作者Rajveer Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:54:19