What would be the best way to refactor this code?
class CoursesController extends Controller
{
public function index(Request $request)
{
//http://localhost:8000/courses?search=logis&lat=53.559003&lng=-2.077371&radius=5
$radius = ($request->has('radius') ? $request->get('radius') : 10);
$lat = ($request->has('lat') ? $request->get('lat') : 53.540930);
$lng = ($request->has('lng') ? $request->get('lng') : -2.111366);
$query = \DB::table('courses')
->join('locations', 'courses.id', '=', 'locations.course_id')
->join('providers', 'courses.provider_id', '=', 'providers.id')
->select('courses.id', 'title', 'courses.description', 'locations.name', 'locations.address', 'providers.company_name', 'providers.company_name', 'providers.logo', \DB::raw('3959 * acos( cos( radians('.$lat.') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians ('.$lng.') ) + sin( radians('.$lat.') ) * sin( radians( lat ) ) ) as distance') );
if($request->has('search'))
$query->where('courses.title', 'LIKE', '%' . $request->get('search') . '%');
if($request->has('difficulty_level'))
$query->where('courses.difficulty_level', '=', $request->get('difficulty_level'));
if($request->has('parking'))
$query->where('locations.parking', '=', 1);
if($request->has('accessibility'))
$query->where('locations.accessibility', '=', 1);
if($request->has('public_transport'))
$query->where('locations.public_transport', '=', 1);
$courses = $query->having('distance', '<', $radius)
->orderBy('distance', 'asc')
->get();
$categories = Category::orderBy('category')->lists('category', 'id');
return view('frontend.courses.index', compact('courses', 'categories'));
}