如何将数组变量从控制器传递到视图(Laravel 5)?

时间:2016-01-22 07:29:20

标签: php laravel laravel-5

我的数组($ response_array)是这样的:

Array
(
    [Auth] => Array
        (
            [UserID] => test
            [UserIP] => 123
            [XmlVersion] => Testing Version
        )

    [SearchAvailRequest] => Array
        (
            [CountryCd] => ID
            [CityCd] => JOG
            [CheckIn] => 2016-01-08
            [CheckOut] => 2016-01-09
        )

    [SearchAvailResponse] => Array
        (
            [Hotel] => Array
                (
                    [0] => Array
                        (
                            [HotelNo] => 1
                            [HotelCode] => 321
                            [HotelName] => Test 1 Hotel
                            [RmGrade] => Deluxe
                            [RmGradeCode] => DLX
                        )

                    [1] => Array
                        (
                            [HotelNo] => 2
                            [HotelCode] => 212
                            [HotelName] => Test 2 & 1 Hotel
                            [RmGrade] => Deluxe
                            [RmGradeCode] => DLX
                        )

                )

        )

)

我想发送酒店数据以查看

所以视图显示数据如下:

Hotel Name
Check In
Check Out
City

我试着这样:

控制器:

  ...
   return view('frontend.hotel.main_list_hotel',[
  'hotel' => $response_array['SearchAvailResponse']['Hotel']
]);
    ...

查看:

 @foreach($hotel as $key=>$value)
{{ $key }}
{{ $value['HotelNo'] }}                     
@endforeach

我得到酒店没有& amp;酒店名称

如何办理入住,退房,城市和国家?

有关如何解决此问题的任何建议?

非常感谢

3 个答案:

答案 0 :(得分:2)

你可以做这样的事情

return view('frontend.hotel.main_list_hotel', [
    'availability' => [
        'checkIn' => $response_array['SearchAvailRequest']['CheckIn'],
        'checkOut' => $response_array['SearchAvailRequest']['CheckOut'],
        'city' => $response_array['SearchAvailRequest']['CityCd'],
        'country' => $response_array['SearchAvailRequest']['CountryCd'],
        'hotels' => $response_array['SearchAvailResponse']['Hotel'],
    ]
]);

答案 1 :(得分:2)

您可以使用与SearchAvailRequest数组相同的方式传递SearchAvailResponse数组:

 ...
   return view('frontend.hotel.main_list_hotel',[
  'hotel' => $response_array['SearchAvailResponse']['Hotel'], 
  'city_info' => $response_array['SearchAvailRequest']
]);

答案 2 :(得分:0)

为什么不试试with() Laravel方法?

return view('frontend.hotel.main_list_hotel')->with(compact(response_array['SearchAvailResponse']['Hotel']));

所有数据都发送到视图,您可以使用:

{{ $response_array['SearchAvailResponse']['Hotel'] }}

在您的刀片模板中。