1

My search function is not returning the results I expect.

How can I output the query object to see what is returned by the db query?

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);
}

I am then trying to output the object into my view as follows

@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

No results are being displayed in my view so I assume my object is empty... I'm very confused and have been stuck on this for ages now

Stretch0
  • 8,362
  • 13
  • 71
  • 133

1 Answers1

0

You never execute the query in $query. Read up: http://laravel.com/docs/5.0/queries

You'll need to specify one of the following:

  • $query->pluck()
  • $query->get()
  • $query->first()
Adam Link
  • 2,783
  • 4
  • 30
  • 44
  • My query is still not returning the expected results. How can I view the SQL query that I send to the database? – Stretch0 Mar 19 '15 at 04:58
  • Based on http://stackoverflow.com/questions/14536165/get-the-query-executed-in-laravel-3-4, its `$queries = DB::getQueryLog(); $last_query = end($queries);` – Adam Link Mar 19 '15 at 05:15