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

Jacobs's avatar

Two posts in one view, determining which controller method to use in routes?

Hey, is it possible to check for variable values from post form in route file and based on them determine which controller method to use?

E.g. pass two variables, $edit and $store, then based on their values determine in routes file which controller method to use? If $edit =1, use test@edit with the post form data, if $store = 1, use test@store with form data.

Thanks in advance.

0 likes
13 replies
bobbybouwmann's avatar

Well that is currently not possible, but something tells me that your use case is not unique and that there is a better solution for this! Can you describe your use case?

Jacobs's avatar

Well, I have a schedule view, which based on data from db shows the current schedule for the current room.

This works completely fine, I can add a new time by clicking on the hour, set duration, start time, capacity, then I can store it in database while checking for conflict - all works fine.

Now, on the same page, I want to be able to click on an existing time and remove it, so the form sends the data of which hour I've clicked on and then it should get into controller and remove it based on it's unique id.

However, I need to use two post forms since I'm sending data on both creating and removing(editing) a time.

martinbean's avatar

@Jacobs You should have two routes. One for creating an entry, one for updating an entry.

Jacobs's avatar

I definitely don't want the admin to have to use two separate pages for creating and for editing, it's bad.

martinbean's avatar

@Jacobs I never said anything about two separate pages?

In your schedule view, you should have some way of adding a new appointment which submits to a “store” route. If editing an existing appointment, submit to an “update” route.

Jacobs's avatar

Mind throwing an example of how would I submit to a different route from a post form? Thank you.

rawilk's avatar

If you really only want to have one single route, you can submit to one controller action, then depending on the variable, call the method you want to from there. I wouldn't recommend that though; it's better practice to use two routes as @martinbean has told you.

1 like
martinbean's avatar

@Jacobs Surely you have two different sub-views for either creating an appointment, or editing an existing one? Even if it’s on the same “calendar” view. You would use the different routes in those sub-views.

Jacobs's avatar

This is, I'd say, admin's view of the schedule - so that includes creating a time, editing/removing a time on the chosen week.

View for user is different, since that part is only for reservations.

martinbean's avatar

@Jacobs Yes. So when creating time submit to the store route, as you’re creating a new resource. When updating a time submit to the update route, as you’re updating an existing resource.

Jacobs's avatar

May I know how would the code look like for those forms?

Cronix's avatar

Create

<form action="/route/to/create" method="post">
    {{ csrf_field() }}
    // .. other fields and submit button
</form>

Edit

<form action="/route/to/update" method="post">
    {{ csrf_field() }}
    {{ method_field('PUT') }}
    <input type="hidden" name="post_id" value="{{ $post->id }}">
    // .. other fields and submit button
</form>

Please or to participate in this conversation.