I have a route like this: www.mysite.com/agenda/2014/08/16?tags=entertainement,sport,information&status=free&author=8
It looks into the agenda the events that precise day, that have at least one of the tags, that have a status of free, and are authored by author 8.
Of course none of the params are mandatory. I could really to something like this: www.mysite.com/agenda?author=15
It would show all events by author 15.
Right now, as I am a beginner developper, I tried to handle all of this in the controller: it is obviously not the way to go.
I do something like this:
$agenda = Agenda::with('schedule');
if(Input::has('author')) {
$agenda = $agenda->where('author', '=', Input::get('author'));
}
$agenda = $agenda->get();
For tags and dates, it's more complicated because I have to look into multiple tables, parse the input, etc, it's becoming a mess sooner than later!
Someone said I should "make a class to handle this", but I need more involved guidance there, I'm quite lost.
How to you guys do this kind of things?
I think he/she meant to create a repository class.
Your controller would look something like
<?php
use Vanpet\Repositories\AgendaRepositoryInterface;
class AgendaController extends Controller {
protected $agenda;
public function __construct(AgendaRepositoryInterface $agenda)
{
$this->agenda = $agenda;
}
public function show($year, $month, $day)
{
if($this->agenda->findByDate($year, $month, $day, Input::all()) {
return View::make('agenda.show', $agenda);
} else {
return View::make('error.page');
}
}
and then in your repository you would do all the stuff necessary to return the correct angenda(s)
Above might be a lot of info for a beginner but let me know if it makes sense.
Ok I will create a repository.
I fear I may have to come back for more help with the findByDate method, but that will be for later!
Thanks!
vanpet said:
Ok I will create a repository.
I fear I may have to come back for more help with the findByDate method, but that will be for later!
Thanks!
findByDate is your own method in the repository, you can name it whatever you like
Yeah, but it's the inside of that method that will be hard to code, I guess ;-)
vanpet said:
Yeah, but it's the inside of that method that will be hard to code, I guess ;-)
try to create an AgendaRepository, an AuthorRepository, a TagRepository and etc and then use your TagRepository in your AgendaRepository
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community