通过在路由中设置,通过 url 传递表单数据

Pass form data through url by setting in route

我在我的 Laravel 项目中设置了一条路线,如果它是 href 我会这样做 {{route('destination'),$country_from,$country_to}} 但现在我想将表单数据作为 $country_from$country_to 请问我该怎么做

<form action="{{route('destination')}}">
            @csrf
                        <input type="text"
                            class="search-form flexdatalist"
                            name="origin"
                            placeholder="country_from"
                            data-search-in='name'
                            data-min-length='1'
                            />


                        <img src="{{URL::asset('img/location.svg')}}" class="search-icon origin" width="28px" height="21px"/>
                    </div>
                    <div class=" col-md-3 mx-0 px-0">
                           <input type="text"
                                class="search-form flexdatalist"
                                name="country_to"
                                data-search-in='name'
                                data-min-length='1'
                                placeholder="Travelling to"

                            />
                            <img src="{{URL::asset('img/location.svg')}}" class="search-icon destination" width="28px" height="21px"/>
                    </div>
                    <div class="col-md-3 mx-0 px-0">
                    <input type="text"
                                class="search-form flexdatalist"
                                name="residence"
                                data-search-in='name'
                                data-min-length='1'
                                placeholder="Residing In"

                            />
                            <img src="{{URL::asset('img/location.svg')}}" class="search-icon residence" width="28px" height="21px"/>
                    </div>
                    <div class="col-md-3 px-0">
                        <input type="submit" value="Search Visas" class="btn btn-primary search-btn"/>
                        </form>

路线

Route::get('/{country_from}/{country_to}',  'DestinationCountriesController@index')->name('destination');

我不太明白你想做什么:

您想在表单提交后将用户重定向到 URL,并在重定向中使用表单值 URL?

  • 在您的表单 POST 控制器中,从 $request 和 return 检索输入 return redirect()->route('route_name', ['country_from' => $request->country_from, 'country_to' => $request->country_to])

您想使用 URL 值作为输入值,因此在页面加载时,输入会填充 URL 值吗?

  • 您的 "destination" 路由控制器方法接受参数 $country_from$country_to,因为您在路由中声明了这些变量。你的控制器中有这些变量,你可以清理它们,将它们绑定到 returned 视图(例如:return view('view', $data)return view('view', compact('country_from', 'country_to'))),并像往常一样在你的 blade.
  • 您还可以使用 \Request::route('country_from') 访问 URL 值。在将其用作输入之前,您应该清理这些值。
  • 在 blade 中使用这些变量作为输入 value 属性,或 placeholder,任何 {{$country_to}}.