Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Danieloplata's avatar

Single route - multiple actions

Hi, sorry if this is quite a basic/dumb question. So far from the Laravel from Scratch tutorials, I have created routes and models with a single function - like a

Route::get('/projects/create', 'ProjectController@create')->name('createProject');

Which then runs the create function of my ProjectController

My question is: How do I perform multiple actions based on a single route?

For example:

If I created the following route

Route::get('/start/{panelid}/{respondentid}'`

How would I go about both

1 - Creating a Respondent in the respondent table

2 - Looking up the {PanelID} in the database (in this case a URL stored in the database)

3 - Redirecting to that link?

0 likes
8 replies
Cronix's avatar

You wouldn't want to use a get route to create something. You'd use a post route.

I'm not clear on why you are passing the respondentid if you are creating the respondent. How do you know the ID if it hasn't been created yet?

Danieloplata's avatar

Hi Cronix,

I am assuming a post route can still access the variables set in the route?

(don't be confused by my route example, the create method simply gets the view to create a project. I have a seperate 'store' method to actually save it.)

My project serves as a middleman, so a respondent already exists on someone else's database. I'm passing the ID via the URL to record it along with a bit of additional info like their ip/country/useragent/status.

Let's say I wanted to create a project and a respondent via the same link, is it as simple as having 2 routes which reference the same URL? e.g.

Route::post('/start/{projectID}/{respondentID}', 'ProjectController@store')->name('storeProject');
Route::post('/start/{projectID}/{respondentID}', 'RespondentController@store')->name('storeRespondent');
Cronix's avatar

I am assuming a post route can still access the variables set in the route?

Sure, you just have to specify them in the method

public function create(Request $request, $projectID, $respondentID) {

}

Let's say I wanted to create a project and a respondent via the same link, is it as simple as having 2 routes which reference the same URL? e.g.

No. You can't have 2 routes using the same verb going to the same endpoints (/start/{projectID}/{respondentID}). Laravel will match whichever one comes first in the route file and it would never reach the 2nd one.

Danieloplata's avatar

Obviously this is something quite simple in vanilla PHP that is quite difficult with OOP/Frameworks. I would just make multiple SQL queries on the same page.

Do you have any suggestions to how I can achieve both actions? (1. storing a respondent and 2. SQL lookup for a link)

Cronix's avatar

You can do however many things you want in there

public function create(Request $request, $projectID, $respondentID) {

    // create a new respondent
    $repondent = new Respondent; // I assume you have a Respondent model
    $respondent->name = $request->name; // get the name from request
    // ...other fields to save
    $respondent->save(); // save it to the db

    // retrieve the project
    $project = Project::findOrFail($projectID); // will 404 if not found

    // redirect to some url using the url from the $project
    return redirect('/project/'. $project->url);    
}
Snapey's avatar

Obviously this is something quite simple in vanilla PHP that is quite difficult with OOP/Frameworks. I would just make multiple SQL queries on the same page.

I have news for you. What you put in your classes is PHP !

If you want to run two queries in the same controller method, what is stopping you?

2 likes
Danieloplata's avatar

Thanks Cronix. I had understood to create the code in such a way violated MVC rules.

Sorry Snapey, my understanding of classes is very weak. I came from 100% procedural style with no classes or functions. I have difficulties understanding the flow of information.

In vanilla PHP I would basically make a couple of SQL queries, and a header redirect and wrap it in an if(isset.. on the GET variables.

The concept of passing information to classes is still odd to me.

jlrdw's avatar

You should take Jeffrey"s intro php videos, he covers MVC, OOP, And classes. It's simple, but coming from procedural it will give you a better understanding.

Browse around here: https://laracasts.com/skills/php

Please or to participate in this conversation.