Home > Software design >  How can I pass part of an url to my controller in Laravel
How can I pass part of an url to my controller in Laravel

Time:08-11

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.

CodePudding user response:

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']);

CodePudding user response:

I reckon you need to have your route just as Route::get('/edit/{id}', [TestController::class, 'edit']);.

Please feel free to look at my laravel 7 snippet here, https://phpsandbox.io/n/73314638-5fvdc. It demonstrates below:

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

Note, I borrowed the default welcome.blade.php file, and updated the default / route in web.php.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    public function edit($id)
    {
        return view('welcome', ['id' => $id]);
    }
}
  • Related