Exercise - a Single Parameter Route
Create a Laravel app that defines a single parameter route. The route is used to display a single record, based on a route's parameter value. The exercise has the following tasks:
- Create a Laravel project.
- Create a table.
- Create a model.
- Define a single parameter route.
- Fetch a single record whose id column matches the parameter's value.
- Create a view.
- Display the record in a browser.
Creating a Laravel project
Create a Laravel project and start a local server. In the terminal, we write:
laravel new laravel-single-parameter
We can go with the default Laravel installer options and choose the MySQL database.
Navigate to the project's folder:
cd laravel-single-parameter
And start a local server:
composer run dev
Creating the posts table
In this newly opened terminal, create a migration that creates the posts table:
php artisan make:migration create_posts_table
Open the newly created migration file that ends with the create_posts_table.php and modify the pre-filled code inside the Schema::create() function to add the title and post_text columns:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('post_text');
$table->timestamps();
});
Running the migration
To run the migration and create the posts table in our database, in the terminal, we write:
php artisan migrate
Inserting sample records
To quickly insert a few sample records, we start the tinker tool:
php artisan tinker
And paste and execute the following statements:
DB::table('posts')->insert([
[
'title' => 'This is the first post',
'post_text' => "This is the first post's sample text.",
'created_at' => now(),
'updated_at' => now(),
],
[
'title' => 'This is the second post',
'post_text' => "This is the second post's sample text.",
'created_at' => now(),
'updated_at' => now(),
],
[
'title' => 'This is the third post',
'post_text' => "This is the third post's sample text.",
'created_at' => now(),
'updated_at' => now(),
],
]);
exit;
Creating a model for the posts table
To create a model for the posts table, in the terminal, we run:
php artisan make:model Post
Creating a single parameter route
Now, we will:
- Define a single parameter route.
- Define a callable that handles that route.
- Fetch a single record whose id column value matches the parameter's
{id}value. - Pass that record to a view.
Open the web.php file and paste the following code:
<?php
use App\Models\Post;
use Illuminate\Support\Facades\Route;
Route::get('/showpost/{id}', function (string $id) {
$myPost = Post::where('id', $id)->firstOrFail();
return view('postview', ['myPost' => $myPost]);
});
Creating a view
In the terminal, create a postview blade template that will hold the HTML code for our route:
php artisan make:view postview
Open the newly created resources/views/postview.blade.php file and paste the following HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Laravel Exercise - A Single Parameter</title>
</head>
<body>
<h1>Laravel Exercise</h1>
<p>Showing a single record whose id matches the parameter's value:</p>
<h2>{{ $myPost->id }}</h2>
<h3>{{ $myPost->title }}</h3>
<p>{{ $myPost->post_text }}</p>
</body>
</html>
This HTML template shows a single record whose id column matches the routes {id} parameter value. The record is stored inside the $myPost variable. We print the record's id, title and post_text columns using multiple {{ }} blade echo statements
Displaying the results in a browser
Finally, if we access the route in our browser and provide a parameter value of, for example, 2, as in http://127.0.0.1:8000/showpost/2, we see the following result in our web browser: