🚀 How to Pass Data from One URL to Another in Laravel

NatureNature
3 min read

"How do I pass data from one page (URL) to another?"

Whether it's a form submission, a dynamic link, or redirecting with values — Laravel makes it super simple. In this post, I’ll walk you through how to pass data via URL in Laravel, how to retrieve it, and where to write the code. 🙌


🧠 What You’ll Learn

  • How to create routes that pass data

  • How to send data via URL (GET)

  • How to retrieve that data in a controller or view

  • Real-world example with step-by-step code

  • What the output will look like


🛠 Step 1: Set Up a Fresh Laravel Project (Optional)

If you're just testing this out:

laravel new url-passing-demo
cd url-passing-demo
php artisan serve

Or use an existing Laravel project.


📁 Step 2: Create Two Routes in web.php

Let's say we want to go from:

Open routes/web.php and add:

use Illuminate\Support\Facades\Route;

Route::get('/user/{name}/{age}', function ($name, $age) {
    return view('user', ['name' => $name, 'age' => $age]);
});

What’s happening here?

  • {name} and {age} are route parameters.

  • When you visit this URL → localhost:8000/user/John/21, it grabs:

    • $name = John

    • $age = 21

  • Then it sends them to the user.blade.php view.


🧾 Step 3: Create a View File (resources/views/user.blade.php)

Now let’s display that data:

<!-- resources/views/user.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <title>User Info</title>
</head>
<body>
    <h1>Hello {{ $name }}, your age is {{ $age }}!</h1>
</body>
</html>

✅ Output when you visit http://localhost:8000/user/John/21:

Hello John, your age is 21!

💡 You can change the name and age directly in the URL:
/user/Aisha/30 → "Hello Aisha, your age is 30!"


Let’s say you’re on a home page and want to redirect to a user page with data.

In web.php:

Route::get('/', function () {
    $name = "Rahul";
    $age = 25;
    return redirect()->route('user.info', ['name' => $name, 'age' => $age]);
});

Route::get('/user/{name}/{age}', function ($name, $age) {
    return view('user', compact('name', 'age'));
})->name('user.info');

✅ Here, we’re:

  • Defining a named route: user.info

  • Redirecting to that route with parameters name and age


🎯 Bonus: Passing Data Using Query String (e.g., ?name=John&age=22)

In web.php:

Route::get('/show', function (Illuminate\Http\Request $request) {
    $name = $request->query('name');
    $age = $request->query('age');
    return "Hi $name, you are $age years old!";
});

Now visit:
➡️ http://localhost:8000/show?name=Ravi&age=22
📋
Output:

Hi Ravi, you are 22 years old!

📌 Summary

MethodHow?Used For
Route Parameters/user/{name}/{age}Clean, dynamic URLs
Query Strings/show?name=Ali&age=30Filters, searches
Redirect with route()redirect()->route('user', [...])Passing variables while redirecting
View with compact/arrayview('user', ['name' => ..., 'age' => ...])Passing to Blade views

🎁 Real-Life Use Case

Imagine you're on a user list page, and when you click on a user, you want to go to a details page:

<!-- user-list.blade.php -->
@foreach($users as $user)
    <a href="{{ url('/user/'.$user->name.'/'.$user->age) }}">
        {{ $user->name }}
    </a><br>
@endforeach

Clicking that will take you to:
/user/John/23 → and show "Hello John, your age is 23!"


✅ Final Thoughts

Laravel makes it easy to pass data through routes using:

  • Route Parameters (clean and RESTful)

  • Query Strings (for optional values or filters)

  • Redirects and Named Routes (for structured control)

Once you get the hang of this, you’ll start building dynamic pages, profile views, product details, and much more in no time. 💪


10
Subscribe to my newsletter

Read articles from Nature directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nature
Nature