Little background ...
I have two models in my application that are "local". I could very easily write a query in the repository to return what I need, however I am sure that this can be done much better with Laravel Scopes and Traits, etc. (Maybe I'm wrong).
So, I came up with the following solution:
An example of an abstract model
abstract class AbstractModel extends Model implements SomeOtherStuff
{
public function scopeNearby(Builder $query)
{
return $query->selectRaw('
(
6373000 * acos(
cos( radians( ' . $this->location->latitude . ' ) )
* cos( radians( X(' . $this->location->table . '.location) ) )
* cos( radians( Y(' . $this->location->table . '.location) ) - radians( ' . $this->location->longitude . ' ) )
+ sin( radians( ' . $this->location->latitude . ' ) )
* sin( radians( X(' . $this->location->table . '.location) ) )
)
) AS distance
')
->join($this->location->table, $this->location->primaryKey, '=', $this->primaryKey);
}
}
User Model Example
class User extends AbstractModel implements SomeOtherStuff
{
public function location()
{
return $this->hasOne(‘App\Models\User\Location', 'user_id');
}
// All other model stuff here
}
User Controller Example
class UserController extends AbstractController
{
public function nearby()
{
$users = $this->shield->user()->nearby()->toSql();
echo $users;
}
}
My problems
The above solution works, however, it is very wrong (in my opinion)! The area nearby()should be available only for models that are "local." Naturally, I thought the best solution would be to implement Trait as follows:
trait Locatable
{
public function scopeNearby(Builder $query)
{
}
}
, , , protected Trait...
()
, , . :
- , , , - ?
- ,
->with('location') ? ? , , ...