Skip to main content
Tweeted twitter.com/StackCodeReview/status/1473760281432047627
added 1 character in body
Source Link
user142213
user142213
<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Eventary;


class EventaryEventsController extends Controller
{

    // function for the /kursangebote/{$course} pages
    public function showEvents($course)
    {
        // filter the courses and set the views and id
        if ($course == 'Workshops''workshops') {
            $view   = 'pages.course.view-1';
            $id     = '1';
        }

        if ($course == 'Events''events') {
            $view   = 'pages.course.view-2';
            $id     = '2';
        }

        if ($course == 'Salsa''salsa') {
            $view   = 'pages.course.view-3';
            $id     = '3';
        }

        if ($course == 'dance4') {
            $view   = 'pages.course.view-4';
            $id     = '4';
        }

        if ($course == 'dance5') {
            $view   = 'pages.course.view-5';
            $id     = '5';
        }

        if ($course == 'dance6') {
            $view   = 'pages.course.view-6';
            $id     = '6';
        }

        if ($course == 'dance7') {
            $view   = 'pages.course.view-7';
            $id     = '7';
        }

        if ($course == 'dance8') {
            $view   = 'pages.course.view-8';
            $id     = '8';
        }

        // get the course data from the database
        $events = Eventary::query()
            ->orderBy('title')
            ->orderBy('start')
            ->where('category', $id)
            ->where('start', '>', Carbon::now())
            ->get();

        // pass through the data to the correct views
        return view($view, [
            "events" => $events
        ]);

    }


    // function for the /events page
    public function workshopsList()
    {
        // get the course data from the database
        $events = Eventary::query()
            // show all events with the category 1 (events & workshops)
            ->where('category', '1')
            ->where('start', '>', Carbon::now())
            ->get();


        // pass through the data to the correct views
        return view('pages.course.event-list', [
            "events" => $events
        ]);

    }


    // function for the /kalender page
    public function calendarList()
    {
        // get the course data from the database
        $events = Eventary::query()
            // show all events with the category 1 (events & workshops)
            ->where('start', '>', Carbon::now())
            ->get();


        // pass through the data to the correct views
        return view('pages.calendar', [
            "events" => $events
        ]);

    }


    // function for fullcalendar json generation
    public function feed()
    {
        // get database
        $events = DB::table('eventaries')->get();

        return json_encode($events, JSON_PRETTY_PRINT);

    }



}
<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Eventary;


class EventaryEventsController extends Controller
{

    // function for the /kursangebote{$course} pages
    public function showEvents($course)
    {
        // filter the courses and set the views and id
        if ($course == 'Workshops') {
            $view   = 'pages.course.view-1';
            $id     = '1';
        }

        if ($course == 'Events') {
            $view   = 'pages.course.view-2';
            $id     = '2';
        }

        if ($course == 'Salsa') {
            $view   = 'pages.course.view-3';
            $id     = '3';
        }

        if ($course == 'dance4') {
            $view   = 'pages.course.view-4';
            $id     = '4';
        }

        if ($course == 'dance5') {
            $view   = 'pages.course.view-5';
            $id     = '5';
        }

        if ($course == 'dance6') {
            $view   = 'pages.course.view-6';
            $id     = '6';
        }

        if ($course == 'dance7') {
            $view   = 'pages.course.view-7';
            $id     = '7';
        }

        if ($course == 'dance8') {
            $view   = 'pages.course.view-8';
            $id     = '8';
        }

        // get the course data from the database
        $events = Eventary::query()
            ->orderBy('title')
            ->orderBy('start')
            ->where('category', $id)
            ->where('start', '>', Carbon::now())
            ->get();

        // pass through the data to the correct views
        return view($view, [
            "events" => $events
        ]);

    }


    // function for the /events page
    public function workshopsList()
    {
        // get the course data from the database
        $events = Eventary::query()
            // show all events with the category 1 (events & workshops)
            ->where('category', '1')
            ->where('start', '>', Carbon::now())
            ->get();


        // pass through the data to the correct views
        return view('pages.course.event-list', [
            "events" => $events
        ]);

    }


    // function for the /kalender page
    public function calendarList()
    {
        // get the course data from the database
        $events = Eventary::query()
            // show all events with the category 1 (events & workshops)
            ->where('start', '>', Carbon::now())
            ->get();


        // pass through the data to the correct views
        return view('pages.calendar', [
            "events" => $events
        ]);

    }


    // function for fullcalendar json generation
    public function feed()
    {
        // get database
        $events = DB::table('eventaries')->get();

        return json_encode($events, JSON_PRETTY_PRINT);

    }



}
<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Eventary;


class EventaryEventsController extends Controller
{

    // function for the /kursangebote/{$course} pages
    public function showEvents($course)
    {
        // filter the courses and set the views and id
        if ($course == 'workshops') {
            $view   = 'pages.course.view-1';
            $id     = '1';
        }

        if ($course == 'events') {
            $view   = 'pages.course.view-2';
            $id     = '2';
        }

        if ($course == 'salsa') {
            $view   = 'pages.course.view-3';
            $id     = '3';
        }

        if ($course == 'dance4') {
            $view   = 'pages.course.view-4';
            $id     = '4';
        }

        if ($course == 'dance5') {
            $view   = 'pages.course.view-5';
            $id     = '5';
        }

        if ($course == 'dance6') {
            $view   = 'pages.course.view-6';
            $id     = '6';
        }

        if ($course == 'dance7') {
            $view   = 'pages.course.view-7';
            $id     = '7';
        }

        if ($course == 'dance8') {
            $view   = 'pages.course.view-8';
            $id     = '8';
        }

        // get the course data from the database
        $events = Eventary::query()
            ->orderBy('title')
            ->orderBy('start')
            ->where('category', $id)
            ->where('start', '>', Carbon::now())
            ->get();

        // pass through the data to the correct views
        return view($view, [
            "events" => $events
        ]);

    }


    // function for the /events page
    public function workshopsList()
    {
        // get the course data from the database
        $events = Eventary::query()
            // show all events with the category 1 (events & workshops)
            ->where('category', '1')
            ->where('start', '>', Carbon::now())
            ->get();


        // pass through the data to the correct views
        return view('pages.course.event-list', [
            "events" => $events
        ]);

    }


    // function for the /kalender page
    public function calendarList()
    {
        // get the course data from the database
        $events = Eventary::query()
            // show all events with the category 1 (events & workshops)
            ->where('start', '>', Carbon::now())
            ->get();


        // pass through the data to the correct views
        return view('pages.calendar', [
            "events" => $events
        ]);

    }


    // function for fullcalendar json generation
    public function feed()
    {
        // get database
        $events = DB::table('eventaries')->get();

        return json_encode($events, JSON_PRETTY_PRINT);

    }



}
Source Link
user142213
user142213

Optimization of a Laravel controller that passes data and views

I am trying to build a website that shows events. And I use the following controller. Please note that the urls ($view and $course) etc. are renamed and are not the ones used on the real website!

Controller:

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Eventary;


class EventaryEventsController extends Controller
{

    // function for the /kursangebote{$course} pages
    public function showEvents($course)
    {
        // filter the courses and set the views and id
        if ($course == 'Workshops') {
            $view   = 'pages.course.view-1';
            $id     = '1';
        }

        if ($course == 'Events') {
            $view   = 'pages.course.view-2';
            $id     = '2';
        }

        if ($course == 'Salsa') {
            $view   = 'pages.course.view-3';
            $id     = '3';
        }

        if ($course == 'dance4') {
            $view   = 'pages.course.view-4';
            $id     = '4';
        }

        if ($course == 'dance5') {
            $view   = 'pages.course.view-5';
            $id     = '5';
        }

        if ($course == 'dance6') {
            $view   = 'pages.course.view-6';
            $id     = '6';
        }

        if ($course == 'dance7') {
            $view   = 'pages.course.view-7';
            $id     = '7';
        }

        if ($course == 'dance8') {
            $view   = 'pages.course.view-8';
            $id     = '8';
        }

        // get the course data from the database
        $events = Eventary::query()
            ->orderBy('title')
            ->orderBy('start')
            ->where('category', $id)
            ->where('start', '>', Carbon::now())
            ->get();

        // pass through the data to the correct views
        return view($view, [
            "events" => $events
        ]);

    }


    // function for the /events page
    public function workshopsList()
    {
        // get the course data from the database
        $events = Eventary::query()
            // show all events with the category 1 (events & workshops)
            ->where('category', '1')
            ->where('start', '>', Carbon::now())
            ->get();


        // pass through the data to the correct views
        return view('pages.course.event-list', [
            "events" => $events
        ]);

    }


    // function for the /kalender page
    public function calendarList()
    {
        // get the course data from the database
        $events = Eventary::query()
            // show all events with the category 1 (events & workshops)
            ->where('start', '>', Carbon::now())
            ->get();


        // pass through the data to the correct views
        return view('pages.calendar', [
            "events" => $events
        ]);

    }


    // function for fullcalendar json generation
    public function feed()
    {
        // get database
        $events = DB::table('eventaries')->get();

        return json_encode($events, JSON_PRETTY_PRINT);

    }



}

there is a lot of code that gets reused. So I think that this is something that I should improve. But are there any other suggestions?