Sunday, March 12, 2017

How to select articles by condition in local scope?

I have Article model and Country model with specified relationships amid:

Article

public function country(){
    return $this-has_one("Country);
}

I need to get Articles where country is "USA", for this I do:

Articles:with("country")->get();

It returns me all articles with related countries.

How to assign local scope for with("country") that to set additional condition?

I tried:

 public function scopeLocation($query)
    {
        $country_name = Location::get());
        if(!empty($country_name)){
             return $query->where('name', '=', $country_name);
       }
    }

But I dont know how to call that in with



via Darama

Advertisement