I have an index method that fetches all events in the database and shows them in a table
Model Event:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
protected $fillable = [
'title',
'description',
'start_datetime',
'end_datetime',
];
public function getTodayEvents()
{
}
public function getFiveDaysEvents()
{
}
}
Route:
Route::group(['middleware' => 'auth'], function() {
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/profile', 'Auth\UserController@profile')->name('profile');
Route::post('/profile', 'Auth\UserController@updateProfile')->name('updateProfile');
Route::get('event/index/{filter}', 'EventController@index')->name('indexEvent');
Route::get('event/create', 'EventController@create')->name('createEvent');
Route::post('event/store', 'EventController@store')->name('storeEvent');
Route::get('event/show/{id}', 'EventController@show')->name('showEvent');
Route::get('event/edit/{id}', 'EventController@edit')->name('editEvent');
Route::post('event/update/{id}', 'EventController@update')->name('updateEvent');
Route::get('event/destroy/{id}', 'EventController@destroy')->name('destroyEvent');
});
EventController: '
public function index()
{
$events = Event::all();
return view('event.index')->with('events', $events);
}
In this table that displays the events I have a dropdown menu to display all events, today's events and events for the next 5 days
View index:
@extends('layouts.app')
@section('title', 'Eventos')
@section('content')
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">Eventos</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-11">
<a class="btn btn-primary" href="{{ route('createEvent') }}" role="button">Novo Evento</a>
</div><!-- /.col-lg-6 -->
<div class="col-lg-1">
<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Filtrar <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="{{ route('indexEvent', ['filter' => 'all']) }}">Todos os eventos</a></li>
<li><a href="{{ route('indexEvent', ['filter' => 'today']) }}">Eventos de hoje</a></li>
<li><a href="{{ route('indexEvent', ['filter' => 'five']) }}">Eventos nos próximos 5 dias</a></li>
</ul>
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</div>
<table class="table table-bordered">
<tr>
<th>Título</th>
<th>Detalhes</th>
<th>Alterar</th>
<th>Excluir</th>
</tr>
@foreach($events as $event)
<tr>
<td>{{ $event->title }}</td>
<td>
<a href="{{ route('showEvent', $event->id) }}" class="fa fa-file-text-o" aria-hidden="true"></a>
</td>
<td>
<a href="{{ route('editEvent', $event->id) }}" class="fa fa-pencil" aria-hidden="true"></a>
</td>
<td>
<a href="{{ route('destroyEvent', $event->id) }}" class="fa fa-trash" aria-hidden="true"></a>
</td>
</tr>
@endforeach
</table>
</div>
</div>
@endsection
So far so good, my question is that I have both methods getTodayEvents()
and getFiveDaysEvents()
and I would like to know how I could select the method I want from the link in view
and use only a index
in EventController instead of creating 3 methods index
each with a method of model
Event
?