Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danny620's avatar

Complex Query

Good Morning,

I have two tables 'Courses' & 'Locations'

Courses id | company_id | title | intro | description | tags | difficulty_level

Locations id | course_id | venue_name | address_line_1 | address_line_2 | city_town | postcode | lat | lng | spaces | parking | accessibility | public_transport

I want to be able to find a course that is within a certain latitude and longitude mile radius and then filter the course on "parking", "accessibility", "public_transport", "tags", "difficulty_level".

Then display the top 3 results next to the course like example below http://viewingcloud.co.uk/example-search.png

I have some of this working but I want to be able to list a course

this is what I have so far

public function index(Request $request) {

    $search         = $request->get('search');
    $category       = $request->get('category');
    $location       = $request->get('location');
    $entry          = $request->get('entry');
    $beginner       = $request->get('beginner');
    $intermediate   = $request->get('intermediate');
    $advanced       = $request->get('advanced');
    $lat            = $request->get('lat');
    $lng            = $request->get('lng');
    $radius         = $request->get('radius');
    $parking        = $request->get('parking');
    $accessibility  = $request->get('accessibility');
    $transport      = $request->get('transport');

    $courses = Course::active()
               ->search($search)
               //->category($category)
               ->distance($lat, $lng, $radius)
               ->paginate(45);


    /*

    $locations = Location::distance($request->get('lat'), $request->get('lng'))->get();

    foreach ($locations as $location){
        echo $location->venue_name . ' ' . $location->distance . '<br />';
    }

    */

    /*

    $courses = Course::active()
               ->load(['locations' => function ($q) {
                    $q->where('spaces', '>', '15'); //constraint on child
                }])->get();

     foreach ($courses as $course){
        echo $course->title . '<br />';
    }

    */

    /*

    //dd($courses);

    //http://localhost:8000/courses?search=logis&lat=53.559003&lng=-2.077371&radius=5

    $radius = ($request->has('radius') ? $request->get('radius') : 15);
    $lat    = ($request->has('lat') ? $request->get('lat') : 53.540930);
    $lng    = ($request->has('lng') ? $request->get('lng') : -2.111366);

    $query = \DB::table('courses')->level($request->get('difficulty_level'))->where('courses.status', '=', 1)
        ->join('locations', 'courses.id', '=', 'locations.course_id')
        ->join('companies', 'courses.company_id', '=', 'companies.id')
        ->select('courses.id', 'courses.title', 'courses.featured_img', 'courses.intro', 'locations.venue_name', 'locations.city_town', 'locations.start_date', 'companies.company_name', 'companies.username', 'companies.logo', \DB::raw('3959 * acos( cos( radians('.$lat.') ) * cos( radians( locations.lat ) ) * cos( radians( locations.lng ) - radians  ('.$lng.') ) + sin( radians('.$lat.') ) * sin( radians( locations.lat ) ) ) as distance') );

    if($request->has('search'))
        $query->where('courses.title', 'LIKE', '%' . $request->get('search') . '%');

    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', 'search', 'category', 'location', 'lat', 'lng', 'radius', 'entry', 'beginner', 'intermediate', 'advanced', 'parking', 'accessibility', 'transport'));
}
0 likes
0 replies

Please or to participate in this conversation.