简体   繁体   中英

How to loop through this array in Laravel?

I am working on a web application using laravel 5.3, I got one problem.

I want to loop through this array.

What I tried is:

Controller:

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

    }

View is here:

@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

Problem:

Okay problem is when I try to get {{ $f[$key]->monthly_rate }} which is the 2nd array ie RateCards I get nothing. I want to get the Rate Card array and from that array mothly_rate which is a field in there.

Thanks

PS : {{ $f[$key]->monthly_rate }} this return nothings in view.

The problem is that $f[$key] is an array of two objects. That means that the properties youre looking for are inside each of those items, like this:

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

You will have to loop those objects to get their properties values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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