$query = "" if ($request->has('country'){ $query = Post::with("country")->whereHas("country",function($q) use($request){ $q->where("name","=",$request->country); })->get() }else{ $query = Post::with("country")->get(); }