Search code examples
phplaravelurlroutescontroller

How can I pass part of an url to my controller in Laravel


I'm making a registration form where after filling in a form, users get an email with all the info they've submitted including a link where they can edit their registration. Example url: localhost/registrationapp/edit/{id}

I've been trying to pass part of the url to a controller, this is my route:

Route::get('/edit/{id}', [RegistrationappController::class, 'edit'])->with('id', $id);

And I got this function in my controller:

public function edit($id)
    {
        return 123;
        $registration= Registrationapp::find($id);
        return view('edit')->with('registration, $registration);
    }

The return 123 part is just added to see if I can even get to the controller, but it doesn't reach the controller. Instead I'm getting this error when I go to a url (for example localhost/registrationapp/edit/5):

Undefined variable $id

Is there any way to do what I'm trying to do? Any help would be greatly appreciated.


Solution

  • Just remove ->with('id', $id);

    Note : if you are using url : localhost/registrationapp/edit/{id} , make sure to include registrationapp in route :

    Route::get('registrationapp/edit/{id}', [RegistrationappController::class, 'edit']);