How to pass value in url in laravel?

Member

by samara , in category: PHP Frameworks , 21 days ago

How to pass value in url in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , 20 days ago

@samara 

To pass a value in the URL in Laravel, you can use route parameters. Here's how you can do it:

  1. Define a route with a parameter in your routes file (web.php):
1
Route::get('/user/{id}', 'UserController@show');


  1. Define a corresponding controller method that accepts the parameter:
1
2
3
4
5
6
7
public function show($id)
{
    // Retrieve user information based on the ID
    $user = User::find($id);

    return view('user.show', ['user' => $user]);
}


  1. Generate the URL with the parameter in your view or controller like this:
1
$url = route('user.show', ['id' => $user->id]);


  1. You can access the parameter value in your controller method using the $request object:
1
2
3
4
5
6
7
8
public function show(Request $request)
{
    $id = $request->route('id');
    // Retrieve user information based on the ID
    $user = User::find($id);

    return view('user.show', ['user' => $user]);
}


By following these steps, you can pass values in the URL in Laravel using route parameters.