【发布时间】: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]->monthly_rate }} 时,即 RateCards 我什么也没得到。我想获取 Rate Card 数组,并从该数组mothly_rate 中获取一个字段。
谢谢
PS : {{ $f[$key]->monthly_rate }} 这返回什么都没有。
【问题讨论】:
标签: php arrays laravel laravel-5.2 laravel-5.3