我正在逐步解决(总是在你的帮助下)这个Eloquent查询,但还没有完全解决它。很抱歉仍然坚持下去,但我正在努力奋斗。
我已经用两种方式编写了查询。我想保留Laravel一个,因为我使用它,但它是一个不起作用的那个:
当我在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;
以下不工作(我得到了:试图从非对象错误中获取属性)
$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'));
更新:
当然,错误是当它试图从非对象获取属性时
@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: £ {{$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
答案 0 :(得分:1)
您不能简单地将 - &gt; get()链接到原始数据库对象并获得结果。
// Doesn't work
$object->having()->take()->get();
foreach ( $object as $value ) {
// $value is some part of the query builder/eloquent object
}
您必须分配一个新变量
// Works
$results = $object->having()->take()->get();
foreach ( $results as $value ) {
// $value is db result
}