简体   繁体   English

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

[英]How to pass array variable from controller to view (Laravel 5)?

My array ($response_array) is like this : 我的数组($ 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
                        )

                )

        )

)

I want to send the hotel data to view 我想发送酒店数据以查看

So the view display data like this : 因此,视图显示数据如下:

Hotel Name
Check In
Check Out
City

I try like this : 我这样尝试:

Controller : 控制器:

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

View : 查看:

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

I get hotel no & hotel name 我得到酒店编号和酒店名称

How to get check in, check out, city and country? 如何办理入住,退房,城市和国家/地区?

Any suggestions on how I can solve this problem? 关于如何解决这个问题有什么建议吗?

Thank you very much 非常感谢你

You can do something like this 你可以做这样的事情

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

You can pass the SearchAvailRequest array, in the same manner that you did the SearchAvailResponse array: 您可以按照与SearchAvailRequest数组相同的方式SearchAvailResponse数组:

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

Why not try the with() Laravel method ? 为什么不尝试使用with() Laravel方法呢?

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

All datas are sending to view and you can use : 所有数据正在发送以供查看,您可以使用:

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

In your blade template. 在刀片模板中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM