Laravel pass array in route

跟風遠走 提交于 2021-01-07 02:27:05

问题


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:

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.


回答1:


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 $_GET parameters - /data?min=12&max=123&week=1&month=123




回答2:


You write your code in the wrong controller.

Your code must be like:

class ExtController extends Controller
{  

public function get()
{
   // your code
}

}



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/49916482/laravel-pass-array-in-route

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!