将查询对象输出到视图中


Output query object into view

我的搜索函数没有返回我期望的结果。

如何输出查询对象以查看数据库查询返回了什么?

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

我的视图中没有显示任何结果,所以我认为我的对象是空的。。。我很困惑,已经在这个问题上坚持了很长时间了

您永远不会在$query中执行查询。阅读:http://laravel.com/docs/5.0/queries

您需要指定以下内容之一:

  • $query->pluck()
  • $query->get()
  • $query->first()