There is a response in json format (the request was sent via Guzzle Laravel, I used a ready-made API), I deserialized it via json_decode.

public function raspget(RaspRequest $request) { $client = new Client([ 'base_uri' => 'http://localhost:8080', ]); $attributes = $request->only(['departure_station', 'arrival_station', 'date']); $response = $client->request('GET', 'https://api.rasp.yandex.net/v3.0/search/', [ 'query' => [ 'apikey' => 'I_WONT_TELL_YOU', 'from' => $attributes['departure_station'], 'to' => $attributes['arrival_station'], 'date' => $attributes['date'], 'lang' => 'ru_RU', 'transport_types' => 'suburban', ] ]); $times = json_decode($response->getBody(), true); return view('rasps.raspgot')->withTimes($times); } 

Since the answer keeps the schedule for the current day, I need to display it in the blade in the table through the foreach loop. I get an error: Undefined Index. How do I get data from the response and output to the blade? The structure of the answer here: https://tech.yandex.ru/rasp/doc/reference/schedule-point-point-docpage/

  • Undefined Index means that you are trying to get an element of an array that does not exist. Perhaps in your template, an attempt to get an element that is not in $times or an error in writing the key - Dmitry Kozlov
  • Show the Blade template code - P. Fateev
  • Normal foreach loop for table output: - WideWood 3:04 pm
  • @foreach ($ times as $ time) - WideWood 3:04 pm
  • {{$ time-> title}} - WideWood 3:04 pm

1 answer 1

Solved a problem!!! Since we know the number of flight schedules that we are looking for, we need to load this number first from the JSON response: $ total = $ times ['pagination'] ['total']; Next, in the blade template, we pass the same answer $ times and the $ total parameter and already in the template we make a for loop to the table:

  @for ($key = 0; $key < $total; $key++) <tr> <td>{{ $times['segments'][$key]['from']['title'] }}{{__(' - ')}}{{$times['segments'][$key]['to']['title'] }}</td> <td>{{ $times['segments'][$key]['departure'] }}</td> <td>{{ $times['segments'][$key]['arrival'] }}</td> </tr> @endfor