๐ How to Pass Data from One URL to Another in Laravel
Hey there! ๐
So, you're building a Laravel project and wondering:
"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:
-
Page 1:
http://localhost:8000/user/John/21
-
To show:
"Hello John, your age is 21"
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!"
๐งญ Step 4: Link to Another URL with Data
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
andage
๐ฏ 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
Method | How? | Used For |
---|---|---|
Route Parameters | /user/{name}/{age} |
Clean, dynamic URLs |
Query Strings | /show?name=Ali&age=30 |
Filters, searches |
Redirect with route() | redirect()->route('user', [...]) |
Passing variables while redirecting |
View with compact/array | view('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. ๐ช
Comments
Post a Comment