dimanche 22 janvier 2017

Laravel Model : filter database result based on conditions

I would like to be able to filter directly from my request if a user is available in the geographical area that he indicated. So I created a function in my User Model:

public function location(){
$function = new Jfunction();
$visitorLocation = Session::get('location');

if(!empty($this->lat && !empty($this->long)))
{
    $distance = round($function->get_distance_m($visitorLocation['lat'], $visitorLocation['lon'], $this->lat, $this->long)/ 1000, 3);
}
else
{
    $distance = 0;
}

if(empty($this->distance))
{
    $dbDistance = 0;
}
else
{
    $dbDistance = $this->distance;
}

if($distance >= $dbDistance)
{
    return true;
}
else
{
    return false;
}}

To call this function in my query, I do: -> has ('location') .... but this returns the following error: "Call to a member function getRelationQuery () on a non-object".

$users = User::has('location')->where(function($q){
                        $q->where('status', 3);
                        $q->orWhere('status', 10);
                        $q->orWhere('status', 11);
                    })->orderBy('created_at', 'desc')->get();

It looks like Laravel does not want to return a TRUE or FALSE value.

Can you help me ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire