Code Duplication    Length = 42-54 lines in 2 locations

src/Http/Controllers/EventVenueController.php 1 location

@@ 30-71 (lines=42) @@
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index(Request $request)
31
    {
32
        $cacheExpireTime = 900; // Set the duration time of the cache (15 min - 900sec)
33
        $countries = Cache::remember('countries', $cacheExpireTime, function () {
34
            return Country::orderBy('name')->pluck('name', 'id');
35
        });
36
37
        $searchKeywords = $request->input('keywords');
38
        $searchCountry = $request->input('country_id');
39
40
        // To show just the veues created by the the user - If admin or super admin is set to null show all the venues
41
        $authorUserId = ($this->getLoggedAuthorId()) ? $this->getLoggedAuthorId() : null;
42
43
        if ($searchKeywords || $searchCountry) {
44
            $eventVenues = DB::table('event_venues')
45
                ->when($authorUserId, function ($query, $authorUserId) {
46
                    return $query->where('created_by', $authorUserId);
47
                })
48
                ->when($searchKeywords, function ($query, $searchKeywords) {
49
                    return $query->where('name', $searchKeywords)->orWhere('name', 'like', '%'.$searchKeywords.'%');
50
                })
51
                ->when($searchCountry, function ($query, $searchCountry) {
52
                    return $query->where('country_id', '=', $searchCountry);
53
                })
54
                ->orderBy('name')
55
                ->paginate(20);
56
        } else {
57
            $eventVenues = EventVenue::
58
                when($authorUserId, function ($query, $authorUserId) {
59
                    return $query->where('created_by', $authorUserId);
60
                })
61
                ->orderBy('name')
62
                ->paginate(20);
63
        }
64
        //dd(DB::getQueryLog());
65
66
        return view('eventVenues.index', compact('eventVenues'))
67
                ->with('i', (request()->input('page', 1) - 1) * 20)
68
                ->with('countries', $countries)
69
                ->with('searchKeywords', $searchKeywords)
70
                ->with('searchCountry', $searchCountry);
71
    }
72
73
    /***************************************************************************/
74

src/Http/Controllers/TeacherController.php 1 location

@@ 35-88 (lines=54) @@
32
     *
33
     * @return \Illuminate\Http\Response
34
     */
35
    public function index(Request $request)
36
    {
37
        $countries = Country::orderBy('countries.name')->pluck('name', 'id');
38
39
        // Get the countries with active teachers - BUG! IF I CACHE JUST A PART OF THE COUNTRIES WHEN I INSERT A NEW TEACHER WITH A COUNTRY THAT IS NOT IN THE CACHE I GET AN ERROR WHEN I'M BACK TO THE INDEX (eg.no index error)
40
        /*    $cacheExpireTime = 900; // Set the duration time of the cache (15 min - 900sec)
41
            $countries = Cache::remember('teachers_countries', cacheExpireTime, function () {
42
                return DB::table('countries')
43
                    ->join('teachers', 'countries.id', '=', 'teachers.country_id')
44
                    ->orderBy('countries.name')
45
                    ->pluck('countries.name', 'countries.id');
46
            });*/
47
48
        // Search keywords
49
        $searchKeywords = $request->input('keywords');
50
        $searchCountry = $request->input('country_id');
51
52
        // To show just the teachers created by the the user - If admin or super admin is set to null show all the teachers
53
        $authorUserId = ($this->getLoggedAuthorId()) ? $this->getLoggedAuthorId() : null; // if is 0 (super admin or admin) it's setted to null to avoid include it in the query
54
55
        // To retrieve all the teachers when the route is teacher.directory, we set the logged user id to null
56
        if (Route::currentRouteName() == 'teachers.directory') {
57
            $authorUserId = null;
58
        }
59
60
        if ($searchKeywords || $searchCountry) {
61
            $teachers = DB::table('teachers')
62
                ->when($authorUserId, function ($query, $authorUserId) {
63
                    return $query->where('created_by', $authorUserId);
64
                })
65
                ->when($searchKeywords, function ($query, $searchKeywords) {
66
                    return $query->where('name', $searchKeywords)->orWhere('name', 'like', '%'.$searchKeywords.'%');
67
                })
68
                ->when($searchCountry, function ($query, $searchCountry) {
69
                    return $query->where('country_id', '=', $searchCountry);
70
                })
71
                ->orderBy('name')
72
                ->paginate(20);
73
        } else {
74
            $teachers = Teacher::
75
            when($authorUserId, function ($query, $authorUserId) {
76
                return $query->where('created_by', $authorUserId);
77
            })
78
            ->orderBy('name')
79
            ->paginate(20);
80
        }
81
82
        return view('teachers.index', compact('teachers'))
83
            ->with('i', (request()->input('page', 1) - 1) * 20)
84
            ->with('countries', $countries)
85
            ->with('searchKeywords', $searchKeywords)
86
            ->with('searchCountry', $searchCountry)
87
            ->with('loggedUser', $authorUserId);
88
    }
89
90
    /***************************************************************************/
91