Laravel pass array in route

Hello all I have code:

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

In routes:
Route::get('/data/{array?}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30754844735f5e44425f5c5c554270575544">[email protected]</a>')->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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just do as you did:

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

Route:
Route::get('/data', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80c5f8f4c3efeef4f2efecece5f2c0e7e5f4">[email protected]</a>')->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

Method 2

You write your code in the wrong controller.

Your code must be like:

class ExtController extends Controller
{  

public function get()
{
   // your code
}

}

Method 3

Pass the data as query string parameters.

Define your route as

Route::get('/data', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="77320f033418190305181b1b120537101203">[email protected]</a>')->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');
    }
}

Method 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?}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e6b565a6d41405a5c4142424b5c6e494b5a">[email protected]</a>')->name('data');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x