2015-03-19 26 views
1

我的搜索功能沒有返回我期望的結果。將查詢對象輸出到視圖中

如何輸出查詢對象以查看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

你永遠不會在$query執行查詢。讀了起來:http://laravel.com/docs/5.0/queries

你需要指定下列之一:

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

我的查詢仍沒有返回預期的結果。如何查看我發送到數據庫的SQL查詢? – Stretch0 2015-03-19 04:58:54

+0

基於http://stackoverflow.com/questions/14536165/get-the-query-executed-in-laravel-3-4,它的'$ queries = DB :: getQueryLog(); $ last_query = end($ queries);' – 2015-03-19 05:15:34