路由中的 Laravel 传递数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49916482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:39:32  来源:igfitidea点击:

Laravel pass array in route

phplaravel

提问by Jadasdas

Hello all I have code:

大家好,我有代码:

{{ route('data', ['min' => 12, 'max' => 123, 'week' => 1, 'month' => 123]) }}

In routes:

在路线中:

Route::get('/data/{array?}', 'ExtController@get')->name('data');

In ExtController:

在 ExtController 中:

class GanttController extends Controller
{  

public function get($array = [], 
Request $request){
   $min = $array['min'];
   $max= $array['max'];
   $week = $array['week'];
   $month = $array['month'];
}

But this is not working, I not get params in array. How I can get params in controller?

但这不起作用,我没有在数组中获取参数。如何在控制器中获取参数?

I tryeid do with function: serialize, but I get error: missing required params of the route.Becuase I have ?in route.

我尝试使用 function: serialize,但出现错误:missing required params of the route.因为我?在途中。

回答by GTCrais

Just do as you did:

就像你做的那样:

{{ route('data', ['min' => 12, 'max' => 123, 'week' => 1, 'month' => 123]) }}

Route:

路线:

Route::get('/data', 'ExtController@get')->name('data');

Controller:

控制器:

class GanttController extends Controller
{  
    public function get(Request $request){
       $min = $request->get('min');
       $max= $request->get('max');
       $week = $request->get('week');
       $month = $request->get('month');
    }
}

Your data will be passed as $_GETparameters - /data?min=12&max=123&week=1&month=123

您的数据将作为$_GET参数传递-/data?min=12&max=123&week=1&month=123

回答by Leo

First of you need to serialize the array:

首先你需要序列化数组:

{{ route('data', serialize(['min' => 12, 'max' => 123, 'week' => 1, 'month' => 123])) }}

Then you can pass it :

然后你可以通过它:

Route::get('/data/{array?}', 'ExtController@get')->name('data');

回答by Sand Of Vega

You write your code in the wrong controller.

您在错误的控制器中编写代码。

Your code must be like:

你的代码必须是这样的:

class ExtController extends Controller
{  

public function get()
{
   // your code
}

}

回答by chanafdo

Pass the data as query string parameters.

将数据作为查询字符串参数传递。

Define your route as

将您的路线定义为

Route::get('/data', 'ExtController@get')->name('data');

in your view

在你看来

{{ route('data', ['min' => 12, 'max' => 123, 'week' => 1, 'month' => 123]) }}

and in your controller

并在您的控制器中

class GanttController extends Controller
{  
    public function get(Request $request){
       $min = $request->get('min');
       $max= $request->get('max');
       $week = $request->get('week');
       $month = $request->get('month');
    }
}