2016-11-05 4 views
1

私はlaravel 5.3を使ってWebアプリケーションを開発していますが、問題が1つあります。Laravelでこの配列をループする方法は?

この配列をループしたいです。私が試した何

は次のとおりです。

コントローラー:

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 

問題:

問題は、第2の配列{{ $f[$key]->monthly_rate }}を取得しようとしたときです。RateCards何も得られません。 レートカードの配列を取得し、そこのフィールドである配列mothly_rateを取得したいと考えています。

おかげ

PS:ビューで{{ $f[$key]->monthly_rate }}この戻りナッシング。

答えて

0

問題は、$ f [$ key]は2つのオブジェクトの配列です。

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

あなたはそのプロパティ値を取得するためにループにこれらのオブジェクトを持つことになります。このように、youreのを探しているプロパティは、これらの項目のそれぞれの内側にあることを意味 。

+0

このエラーが発生しました。非オブジェクトのプロパティを取得しようとしています。 – Alien

関連する問題