pavel
pavel

Reputation: 7

LARAVEL: trying to get a property from a non-object

I am progressively solving (always with your help) this Eloquent query but have not managed to completely solve it. Sorry to still staying at it, but I am struggling to my best.

I have written the query in two flavors. I would like to keep the Laravel one since I am using that but it is that one the one which doesn't work:

WORKS WHEN I DO IT ON PHPMYADMIN SQL WINDOW:

select properties.id, title, city, price, postedat, propertytype,
( 3959 * acos( cos( radians(43) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-1.5) ) + sin( radians(43) ) * sin( radians( lat ) ) ) ) AS distance 
from properties
join addresses
on properties.id_address_fk = addresses.id
where city = 'Biarritz'
HAVING distance < 100
ORDER BY distance 
LIMIT 0, 20;

BELOW DOESN'T WORK (I get a: trying to get property from a non-object error)

 $properties  = DB::table('properties')
        ->join('addresses', 'properties.id_address_fk', '=', 'addresses.id')
        ->select('properties.id', 'title', 'city', 'price', 'postedat', 'propertytype',
        DB::raw('3959 * acos( cos( radians(43) ) * cos( radians( lat ) ) * cos( radians(  lng ) -  radians  (-1.5) )+ sin( radians(43) ) * sin( radians( lat ) ) ) as distance') );


                if (!empty($location)) {
                $properties = $properties->where('location', '=', $location);
                }

                 if (!empty($propertytype)) {
                $properties = $properties->where('propertytype', '=', $propertytype);
                }

                 if (!empty($bedrooms)) {
                $properties = $properties->where('bedrooms', '>=', $bedrooms);
                }

                 if (!empty($transaction)) {
                $properties = $properties->where('transaction', '=', $transaction);
                }

                 if (!empty($minprice)) {
                $properties = $properties->where('price', '>=', $minprice);
                }

                 if (!empty($maxprice)) {
                $properties = $properties->where('price', '<=', $maxprice);
                }


                $properties->having('distance', '<', 100)
                ->orderBy('distance', 'desc')
                ->skip(0)
                ->take(5)
                ->get();
                        return View::make('propertyfound', compact('premium', 'properties'));

UPDATE:

Of course, the errors are when it tries to get property from a non-object

@foreach($properties as $propfound)

            <div class="row premium"> 
            <div class="col-sm-3 col-xs-3">
                <a href="{{URL::route('property', array($propfound->id) )}}" class="thumbnail " >{{ HTML::image("public/css/images/houses/$propfound->houses1")}}</a>
                <h5>Guide price: &pound; {{$propfound->price}}</h5>
                <h6>Bedrooms: {{$propfound->bedrooms}}</h6>
                <h6>Days on site: 4</h6>
                <h6>Sqft: {{$propfound->m2}}</h6>
            </div>   

            <div class="col-sm-9"> 
                <a href="{{URL::route('property', array($propfound->id))}}"><h3>{{$propfound->title}}</h3></a>
                <p>{{$propfound->description}}</p>
                <p class="hidden-xs"><a href="#">More details, 14 photos, floorplan and brochure</a> | <a href="#">Save property</a> | <a href="#">Contact agent</a> | <a href="#">Upgrade listing</a></p>
                <p class="hidden-xs">Marketed by Knight Frank, Richmond. Telephone: 0843 314 8224  <a href="#">BT 4p/min</a> </p>         
            </div>
            </div>
            <hr />      

            @endforeach 

Upvotes: 0

Views: 2936

Answers (1)

Dave
Dave

Reputation: 3658

You can't simply chain ->get() to the original DB object and get results.

// Doesn't work
$object->having()->take()->get();
foreach ( $object as $value ) {
    // $value is some part of the query builder/eloquent object 
}

You have to assign to a new variable

// Works
$results = $object->having()->take()->get();
foreach ( $results as $value ) {
    // $value is db result
}

Upvotes: 1

Related Questions