Skip to content
Advertisement

It is possible to pass 2 differents types of parameters to a Laravel controller?

I already have a GET route with an URI /projects/{id} which displays Infos of a project with a given id. I also have a GET index route (/projects), which shows all my projects.

My problem is that I currently try to create different indexes (for example one which only displays the projects where I am assigned [e.g. on /projects/mines], or the projects which are pending administrator approval [e.g. on /projects/proposals], and still others displays).

So I want to know if I can have two GET routes /projects/{id}and /projects/{display_mode} which will be calling two differents methods of my ProjectController (respectively show and index).

Thanks for your help! 🙂

Advertisement

Answer

You may have one route /projects which returns all projects as default. If there is query parameter like

/projects?displayMode=proposals

then you can apply filters.

In your controller it would look something like this

$projects = Project::query();
if ($request->query('displayMode') == 'proposals')
    $projects->where('pending', true)

return $projects->get();

You can add multiple filters too in the same way

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement