How to pass two variables in HREF in laravel

You can pass two variables to anchor tag (HREF) in Laravel. You can call the url() helper function or you can call the route method with multiple arguments in Laravel view to pass the variable values to anchor tag in Laravel.

Answers 1
  • Pass multiple variables to anchor tag (href) using url() helper in laravel view

    --PATH resources\views\<home>.blade.php
    <a href="{{ url('user/'.$user->id . '/'.$user->name) }}">  Get user detail </a>
    
    0

    Output:

    user/1/summer

    You can pass the multiple variable to url in laravel using url() helper method. To pass the value using $user->id and $user->name variable to URL you have to define web route and get the user details using controller's method. You can get user details as below.

    Route definition

    use App\Http\Controllers\HomeController;
    
    Route::get('user/{id}/{name}', [HomeController::class, 'getUserDetail']);

    Controller's method code to get the user details to pass the value to anchor tag

    $user = User::findOrFail(1);
    return view('home')->with('user', $user);

    You have to get the data (user details) and return to view name on which you want to return data.

  • Back to code snippet queries related laravel

Related queries helps you to understand and may also used for implementation of current code snippet