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?