0

I have a simple foreach loop that loops through the current week dates in Y-m-d format:

@foreach($dates as $date)
   <a href="{{route('consols.index', array_merge(\Request::query(), ['date' => date('Y-m-d', strtotime($date))]))}}">{{date('l', strtotime($date))}}</a>
@endforeach

With above, I can simply click on the anchor link, and the specific date will be appended:

example.org/consols?date=2020-03-04

Now I use this to filter a specific SQL query and I wish to be able to filter for multiple dates. For example:

example.org/consols?date=2020-03-04&date=2020-03-05&date=2020-03-06

Where in above example, I wish to send 3 dates as query parameters.

If I simply append another &date= to the already present ?date, it will only select the last date= in the query string.

How can I, with PHP, send multiple variables in my URL with the same parameter name?

oliverbj
  • 5,771
  • 27
  • 83
  • 178
  • 1
    Does this answer your question? [Passing arrays as url parameter](https://stackoverflow.com/questions/1763508/passing-arrays-as-url-parameter) – CodyKL Mar 04 '20 at 08:51

3 Answers3

0

Add an integer $i

['date'.$i => date('Y-m-d'

You will have date1= &date2= & date3=

ritesh khadka
  • 152
  • 2
  • 13
0

You can add your date as array i.e. date[].

Your URL would look like:

example.org/consols?date[]=2020-03-04&date[]=2020-03-05&date[]=2020-03-06

When you console your query it will give you

{ date: [ '2020-03-04', '2020-03-05', '2020-03-06' ] }
TalESid
  • 2,304
  • 1
  • 20
  • 41
0

I ended up looking at the link CodyKL linked to, and from there saw that I could actually use http_build_query.

This is my solution:


$url = [];
$dates = isset($_GET['date']) ? $_GET['date'] : null;
if($dates){

  foreach($dates as $date){
     $url['date'][] = date('Y-m-d', strtotime($date));
  }

}

$query = http_build_query($url);

Then I can simply just redirect to $query.

oliverbj
  • 5,771
  • 27
  • 83
  • 178