0
0
Laravelframework~30 mins

Route parameters in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Route Parameters in Laravel
📖 Scenario: You are building a simple Laravel web application that shows user profiles based on their user ID in the URL.
🎯 Goal: Create a Laravel route that accepts a user ID as a route parameter and returns a view showing the user ID.
📋 What You'll Learn
Create a route with a parameter named id
Use the Route::get method
Return a view named user passing the id parameter
Define the route parameter in the URL pattern using curly braces
💡 Why This Matters
🌍 Real World
Route parameters are used in web apps to show dynamic content based on URL parts, like user profiles or product pages.
💼 Career
Understanding route parameters is essential for backend web development with Laravel, enabling dynamic and user-friendly URLs.
Progress0 / 4 steps
1
Create a basic route with a parameter
Create a route using Route::get with the URL pattern '/user/{id}' and a closure function that accepts $id as a parameter.
Laravel
Need a hint?

Use Route::get with the URL pattern containing {id} and a closure with $id parameter.

2
Add a variable to hold the user ID
Inside the closure, create a variable called userId and assign it the value of the $id parameter.
Laravel
Need a hint?

Assign the route parameter $id to a new variable $userId.

3
Return a view passing the user ID
Modify the closure to return the view named 'user' and pass the variable userId to the view using compact('userId').
Laravel
Need a hint?

Use return view('user', compact('userId')); to send the variable to the view.

4
Create the user view to display the user ID
Create a Blade view file named user.blade.php in the resources/views folder. Inside it, display the text User ID: followed by the $userId variable using Blade syntax.
Laravel
Need a hint?

Use Blade syntax {{ $userId }} inside an <h1> tag to show the user ID.