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

ziakhan's avatar

The routes getting override the first one

I have two forms in one page. Both of the tables have a separate models. Now i want to make insert to them. But as the tables are separate so there form are too separate. Also the laravel said that you can't duplicate a route which will result override. Now what is the solution for it.

class HomeController extends Controller

{
    public function index()
    {
        return view('index');
    }

    public function make_booking(Request $request){

        $request->validate([
            'date_from' => 'required',
            'date_to' => 'required',
            'booking_time' => 'required',
            'city' => 'required',
            'people' => 'required',
            'mobile' => 'required'

        ]);

        Booking::create($request->all());

//        return redirect()->back()->with('success', 'Booking has been succesfully submitted');

    }//
    public function make_plan_detail(Request $request){

        PlanDetails::create($request->all());
//        return redirect()->back()->with('plan', 'Your plan has been succesfully submitted');

    }
}

this is my route:

Route::post('/', [HomeController::class, 'make_booking'])
    ->name('booking');
Route::get('/', [HomeController::class, 'index']);

in the view there

<form action="{{route('booking')}}" method="POST">
<button type="submit" class="btn btn-success" id="booking">Book Now</button>
                </form>

<form action="{{route('booking')}}" method="POST">
<button type="submit" class="btn btn-success" id="plan_trip">Plan Trip</button>
                </form>
0 likes
11 replies
ziakhan's avatar

as you can see that i already made one route for it

Route::post('/', [HomeController::class, 'make_booking'])
    ->name('booking');

but when i put with the above route

Route::post('/', [HomeController::class, 'make_plan_detail'])
    ->name('booking');

its override the first make_booking method route

ziakhan's avatar

is it possible to send the button id as a variable to index controller?? then we put the function inside another function? well this approach is good or bad

jlrdw's avatar

I strongly suggest read the chapter on routing.

You cannot use '/' twice like that.

1 like
Snapey's avatar

Your solution is two forms and two routes or one form and one route.

If you opt for two forms, bear in mind that if the user fills in both forms then their data in one form will be lost when they click submit on the other form

1 like
Sinnbeck's avatar

@ziakhan Why? It sounds like you are just trying to give yourself a problem here?

ziakhan's avatar

@Sinnbeck its the requirement in which i have two different form and I need one route for that. Thanks

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@ziakhan Why does the url you store to matter? You redirect away from it anyways. No one will ever see the url, unless they check the browsers html source code.

Also.. there is no way to use the same url with two different methods.. When you post to /, laravel has no way of knowing which route you want to use.. They are the same.. / === /

It is (in theory) the same as saying

$POST_slash = [HomeController::class, 'make_plan_detail'];
$POST_slash = [HomeController::class, 'make_booking'];

Would you expect that to work?

If you for some reason MUST do this, then use the same method for a single route, and then in that method, use the proper "submethod"

Route::post('/', [HomeController::class, 'make_something'])
    ->name('booking');

//in controller
public function make_something(Request $request)
{
    if ($request->type === 'booking') {
         return $this->make_booking($request);
     }
     //else
     return $this->make_plan_detail($request);
}

//and in the booking form
<input type="hidden" name="type" value="booking" />
1 like
Snapey's avatar

no one sees the route that you post to so there is no good reason for them to be the same

But, two forms and two submit buttons is bad UX

1 like

Please or to participate in this conversation.