Skip to content

Instantly share code, notes, and snippets.

@fabian71
Created July 25, 2018 04:28
Show Gist options
  • Save fabian71/2d767938dbf353cbb29d599956c3bba0 to your computer and use it in GitHub Desktop.
Save fabian71/2d767938dbf353cbb29d599956c3bba0 to your computer and use it in GitHub Desktop.
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