【问题标题】:Laravel - pass data from a function to another route's functionLaravel - 将数据从一个函数传递到另一个路由的函数
【发布时间】:2017-03-31 05:03:24
【问题描述】:

当前设置:

目前,用户可以搜索调用 post 路由。然后它会根据搜索返回各种内容到路由search.results,在该路由中它在URL 中显示为place/town

如何将latlng 变量从search 函数传递给showResults 函数。我试过->with(),它返回null,并将它们包含在[$place, $town]的当前数组中,但它们随后被包含在URL中。

路线:

Route::post('search', 'Controller@search')->name('search');
Route::get('{place}/{town}', 'Controller@showResults')->name('search.results');

控制器:

搜索功能:

public function search(Request $request)
{
    $place =$request->get('name');
    $lat = $request->get('lat');
    $lng = $request->get('lng');

    return redirect()->route('search.results', [$place, $town]);
}

showResults 函数(目前为空):

public function showResults()
{
  // I want to use lat and lng here.
}

【问题讨论】:

    标签: php laravel laravel-5 laravel-routing


    【解决方案1】:

    您需要使用Laravel Session

    return redirect()
        ->route('search.results', [$place, $town])
        ->with('lng', $lng)
        ->with('lat', $lat);
    

    在你看来

    public function showResults(Request $request)
    {
         $lat = $request->session()->get('lat');
         $lng = $request->session()->get('lng');
    }
    

    【讨论】:

    • 我非常接近!谢谢你。是否有任何原因导致我的纬度值无法发送 - "-1.5490773999999874" 这就是我 dd() search 函数中的变量时显示的内容,但它在 showResults 中返回 null。经度完美吗?
    • 如何发送你的两个变量?你可以像->with('lat', $lat)->with('lng', $lng);那样链接多个with
    • 多个->with() 完美运行。感谢您的帮助!
    猜你喜欢
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多