Created
July 25, 2018 04:28
-
-
Save fabian71/2d767938dbf353cbb29d599956c3bba0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Model | |
public function scopeDistance($subQuery, $latitude, $longitude, $distance) | |
{ | |
//Generating Query | |
$item_distance_query = '* , (3959 * ' . | |
'acos( cos( radians(?) ) ' . //lat | |
'* cos( radians( lat ) ) ' . | |
'* cos( radians( lng ) - radians(?) ) ' . //long | |
'+ sin( radians(?) ) ' . //lat | |
'* sin( radians( lat ) ) ' . | |
') ) as distance'; //distance3 | |
$subQuery->getQuery()->selectRaw($item_distance_query, | |
[$latitude, $longitude, $latitude] | |
); | |
$rawQuery = self::getSql($subQuery); | |
return DB::table(DB::raw("(" . $rawQuery . ") as item")) | |
->where('distance', '<', $distance); | |
} | |
Controller | |
$model = Model::distance($lat, $lng, $distance); | |
$model->paginate($this->limit_page); | |
***** Não acessa (exemplo) $model->category, $model->image | |
ERRO: | |
Undefined property: stdClass::$category | |
Neste exemplo tudo OK | |
$model = Model::all(); | |
***** Acessa normalmente $model->category, $model->image | |
seria por causa do selectRaw no scopeQuery |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment