3
I have an index method that searches all events in the database and shows 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 the events, the events of today and the events of 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 doubt is that I have both methods getTodayEvents()
and getFiveDaysEvents()
and would like to know how you could select the method I want from the link on view
and use only one method index
in Eventcontroller instead of creating 3 methods index
each with a method of model
Event
?
Pass as a parameter in the URL a value that will identify the desired results. See the function documentation
route
– Woss
I even passed route('indexEvent', ['typel' => 'all']) but what about the rest? In the index method? If you know and can post a reply.
– Marcelo
In the method
index
you treat this value with aswitch/case
, for example.– Woss