我的搜索功能没有返回我期望的结果。
如何输出查询对象以查看db查询返回的内容?
public function search()
{
//Save requests to variables
$price = Helper::explodeString(Request::get('price'));
$texts = Helper::explodeString(Request::get('texts'));
$minutes = Helper::explodeString(Request::get('minutes'));
$data = Helper::explodeString(Request::get('data'));
$contract = Request::get('contract');
$phone_option = Request::get('phone-option');
//get all selected carriers
$carriers = array();
foreach(Carrier::all() as $carrier){
if(Request::has($carrier->name)){
$carriers[] = $carrier->id;
}
}
// build query
$query = Plan::whereBetween('price', $price)
->whereBetween('text', $texts)
->whereBetween('minutes', $minutes)
->whereBetween('data', $data)
->whereIn('carrier', $carriers)
->where('contract', $contract)
->where('phone_inlcuded', $phone_option);
return view('planresults')->with('plans', $query);
}
然后我尝试将对象输出到我的视图中,如下所示
@foreach($plans as $plan)
<div class="panel price">
<div class="panel-heading arrow_box text-center">
<h3>{{$plan->Carrier->name}}</h3>
<h3>{{$plan->name}}</h3>
</div>
<div class="panel-body text-center">
<p class="lead" style="font-size:40px"><strong>${{$plan->price}} / month</strong></p>
</div>
<ul class="list-group list-group-flush text-center">
<li class="list-group-item">{{$plan->Carrier->name}}</li>
<li class="list-group-item">{{$plan->contract}} Month term contract</li>
@if($plan->phone_included == 0)
<li class="list-group-item">No phone included</li>
@else
<li class="list-group-item">Phone option included</li>
@endif
</ul>
<div class="panel-footer">
<a class="btn btn-lg btn-block btn-success" href="{{$plan->url}}">BUY NOW!</a>
</div>
</div>
<!-- /PRICE ITEM -->
@endforeach
我的视图中没有显示任何结果,所以我认为我的对象是空的......我很困惑,现在已经被困在这个上很久了
答案 0 :(得分:0)
您永远不会在$query
中执行查询。阅读:http://laravel.com/docs/5.0/queries
您需要指定以下其中一项:
$query->pluck()
$query->get()
$query->first()