Laravel在路线中传递阵列

时间:2018-04-19 08:39:27

标签: php laravel

大家好,我有代码:

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

在路线中:

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

在ExtController中:

class GanttController extends Controller
{  

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

但这不起作用,我没有得到阵列中的参数。我如何在控制器中获得参数?

我尝试使用函数serialize,但是我收到错误:missing required params of the route.因为我在路上有?

4 个答案:

答案 0 :(得分:1)

就像你做的那样:

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

路线:

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

控制器:

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');
    }
}

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

答案 1 :(得分:0)

首先需要序列化数组:

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

然后你可以传递它:

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

答案 2 :(得分:0)

  

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

您的代码必须如下:

class ExtController extends Controller
{  

public function get()
{
   // your code
}

}

答案 3 :(得分:0)

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

将路线定义为

Route::get('/data', 'ExtController@get')->name('data');
在您的视图中

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

并在您的控制器中

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');
    }
}