模型中的Laravel范围,关系不起作用

时间:2015-01-13 10:08:59

标签: laravel eloquent query-builder

我有一个名为owners的表,它是商店所有者的列表,我想查询此表及其关系stores,并获取商店与用户的距离。目前,用户位置是硬编码的,但我计划将其传递给查询范围,如果我可以使其工作。非常感谢任何提示,谢谢!

<?php class Owner extends Eloquent {

protected $table = 'owners';    

public function scopeDistance($query) {

    $lat = '45.529999';
    $lng = '-122.680000';

    return $query->select(DB::raw("*,
                          ( 3959 * acos( cos( radians(?) ) *
                            cos( radians( lat ) )
                            * cos( radians( lng ) - radians(?)
                            ) + sin( radians(?) ) *
                            sin( radians( lat ) ) )
                          ) AS distance"))
            ->setBindings([$lat, $lng, $lat]);

}

public function stores()
{
    return $this->belongsTo('Store','store_id')->Distance();
}

public function storesDist()
{
    $lat = '47';
    $lng = '-124';
    return $this->belongsTo('Store','store_id')->select(DB::raw("*,
                          ( 3959 * acos( cos( radians(?) ) *
                            cos( radians( lat ) )
                            * cos( radians( lng ) - radians(?)
                             ) + sin( radians(?) ) *
                             sin( radians( lat ) ) )
                           ) AS distance"))
             ->setBindings([$lat, $lng, $lat]);
}

<?php

class OwnersController extends \BaseController {


public function getStoresNearUserA()
{

 // This returns  Call to undefined method Illuminate\Database\Query\Builder::Distance()
 return Owner::orderBy('id')->with('stores')->get();

 }

public function getStoresNearUserB()
{

 // This works
 return Owner::orderBy('id')->with('storesDist')->get();

}

***************修订版

RE: First solution

// app/models/BaseModel.php
<?php

class BaseModel extends Eloquent {


public function scopeDistance($query) {

    return $query->orderBy('id');

}


....app/models/Store.Php
<?php 


class Store extends \Eloquent {

    protected $table = 'owners';


....app/models/Owner.Php
<?php 


class Owner extends \BaseModel {

    protected $table = 'owners';

    public function stores()
    {
        return $this->belongsTo('Store','store_id')->Distance();
    }


...app/controllers/OwnerController.php  
<?php

class OwnersController extends \BaseController {



public function getStoreOwners()
{

        return Owner::orderBy('id')->with('stores')->get();
  }

...My Error :
Call to undefined method Illuminate\Database\Query\Builder::Distance()

1 个答案:

答案 0 :(得分:1)

setBindings中有错误 - where是默认设置。因此,只需将第二个参数设置为select,您就会很好:

public function scopeDistance($query, $lat, $lng) 
{    
    return $query->select(DB::raw("*,
                          ( 3959 * acos( cos( radians(?) ) *
                            cos( radians( lat ) )
                            * cos( radians( lng ) - radians(?)
                            ) + sin( radians(?) ) *
                            sin( radians( lat ) ) )
                          ) AS distance"))
            ->setBindings([$lat, $lng, $lat], 'select');
}

您也可以使用

->addBinging($lat, 'select')->addBinding($lng, 'select')->addBinding($lat, 'select');

这是安全的,因为现在,如果有select的任何其他绑定(非常罕见,我承认,但仍然......)你会搞砸查询。

另外请注意,您不能像这样在关系查询中使用Owner模型范围。我建议您在BaseModel中定义此范围,然后OwnerStore模型会对其进行扩展,您可以在两者中使用范围。