【问题标题】:How to loop through this array in Laravel?如何在 Laravel 中循环遍历这个数组?
【发布时间】:2017-03-19 06:00:47
【问题描述】:

我正在使用 laravel 5.3 开发一个 Web 应用程序,但遇到了一个问题。

我想遍历这个数组。

我尝试的是:

控制器:

  public function postSearch(Request $request)
    {

        $categoryid = $request->category_id;
        $locationid = $request->location_id;
        $category = Category::where('id', $categoryid)->first();
        $cities = Location::where('id', $locationid)->pluck('city');
        $cityname = $cities[0];

        $alllocations = [];
        $ratecards = [];
        $locations = [];

        $father = [];

        $i = 0;
        $filteredlocations = $category->locations->where('city', $cityname)->all();
        foreach($filteredlocations as $location){

            $alllocations[$i] = $location->getaddetails;
            $ratecards[$i] = $location->ratecard;
            $i++;
        }

        $father[0] = $alllocations;
        $father[1] = $ratecards;

        return view('ads', compact('father'));

    }

视图在这里:

@foreach($father as  $key => $f)

                     @foreach($f as $key => $arr)

                        @if (isset($arr->adName))
                            <div class="post">

                                {{Html::image($arr->path,'Ad Picture',array('class' => 'ad-image'))}}
                                <a href="{{ url('ads', $arr->location_id)  }}"><h2 class="post-title">{{ $arr->adName }}</h2></a>

                                <p class="description">{{ $arr->description }} </p>

                                <div class="post-footer">
                                    <span class="categories">
                                        <ul class="cats">
                                           <li class="btn btn-default">Price : 
                                            {{ $f[$key]->monthly_rate }}
                                           </li>
                                           <li class="btn btn-default">Status: 
                                               @if($arr->status == 1) 
                                                    <p>Active</p>
                                               @else
                                                    <p>Not-Active</p>
                                               @endif
                                           </li>
                                            <li><a href="{{ url('ads', $arr->location_id)  }}" class="details-page">view details</a></li>

                                        </ul>
                                    </span>
                                </div>

                            </div>
                        @endif
                         @endforeach

                    @endforeach

问题:

好的问题是当我尝试获取第二个数组 {{ $f[$key]-&gt;monthly_rate }} 时,即 RateCards 我什么也没得到。我想获取 Rate Card 数组,并从该数组mothly_rate 中获取一个字段。

谢谢

PS : {{ $f[$key]-&gt;monthly_rate }} 这返回什么都没有。

【问题讨论】:

    标签: php arrays laravel laravel-5.2 laravel-5.3


    【解决方案1】:

    问题在于 $f[$key] 是一个包含两个对象的数组。 这意味着您要查找的属性位于每个项目中,如下所示:

    $f[$key][0]->monthly_rate
    $f[$key][1]->monthly_rate
    

    您必须循环这些对象以获取它们的属性值。

    【讨论】:

    • 我现在收到此错误:尝试获取非对象的属性。
    猜你喜欢
    • 2022-11-18
    • 2010-11-01
    • 2017-09-12
    • 2017-06-07
    • 2020-06-22
    • 2016-04-14
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多