Support the ongoing development of Laravel.io →
Requests Input Eloquent

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?

Last updated 3 years ago.
0
Solution

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.

Last updated 3 years ago.
0

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!

Last updated 3 years ago.
0

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

Last updated 3 years ago.
0

Yeah, but it's the inside of that method that will be hard to code, I guess ;-)

Last updated 3 years ago.
0

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

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

vanpet vanpet Joined 10 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.