Creating a blog is one of the most practical projects for learning and implementing Laravel 11. In this step-by-step guide, I’ll walk you through the process of setting up a basic blog, covering topics like installation, configuration, creating models and controllers, database management, and deploying your application.
Requirements and Setup
Before starting, ensure that you have the following:
- PHP (Version 8.1 or higher)
- Composer
- A code editor (VS Code is recommended)
- MySQL or a similar database
Start by setting up a local development environment if you haven't already.
Installing Laravel 11
To create a new Laravel project, open your terminal and use the following command:
composer create-project laravel/laravel blogNavigate to the newly created directory:
cd blogTo confirm Laravel 11 installed successfully, start the development server:
php artisan serveVisit http://localhost:8000 in your browser to see the default Laravel page.
Configuring the Database
Open the .env file in the root directory and configure your database settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog_db
DB_USERNAME=root
DB_PASSWORD=yourpasswordThen, create a new database called blog_db in MySQL or your preferred database manager.
Setting Up Authentication
Laravel provides a simple way to add authentication using the Laravel Breeze package. To install it, run:
composer require laravel/breeze --devInstall and publish the authentication views with:
php artisan breeze:install
npm install && npm run dev
php artisan migrateThis command sets up registration, login, and password reset pages.
Creating Models and Migrations
Laravel uses Eloquent models to interact with the database. For this blog, we’ll create a Post model with the following attributes: title, body, and user_id.
To generate the model and migration file:
php artisan make:model Post -mIn the database/migrations folder, find the migration file for Post and update it as follows:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->timestamps();
});Run the migration:
php artisan migrateSetting Up Controllers and Routes
Create a PostController to handle the main blog logic:
php artisan make:controller PostControllerIn routes/web.php, add routes for the blog:
Route::get('/posts', [PostController::class, 'index'])->name('posts.index');
Route::get('/posts/create', [PostController::class, 'create'])->name('posts.create');
Route::post('/posts', [PostController::class, 'store'])->name('posts.store');
Route::get('/posts/{post}', [PostController::class, 'show'])->name('posts.show');
Route::get('/posts/{post}/edit', [PostController::class, 'edit'])->name('posts.edit');
Route::put('/posts/{post}', [PostController::class, 'update'])->name('posts.update');
Route::delete('/posts/{post}', [PostController::class, 'destroy'])->name('posts.destroy');Building Views with Blade Templates
In the resources/views directory, create a posts folder. Here, we’ll create templates for listing posts (index.blade.php), showing a single post (show.blade.php), and forms for creating and editing posts (create.blade.php and edit.blade.php).
Example: index.blade.php
@extends('layouts.app')
@section('content')
<h1>Blog Posts</h1>
@foreach ($posts as $post)
<div>
<h2>{{ $post->title }}</h2>
<p>{{ $post->body }}</p>
<a href="{{ route('posts.show', $post->id) }}">Read more</a>
</div>
@endforeach
@endsectionCustomize the other templates similarly to fit your design.
Testing the Blog
After building your blog, test each route to ensure everything functions as expected. Use Laravel’s built-in testing tools to check for functionality. For instance:
php artisan testDeploying the Blog Application
To deploy your Laravel blog, you’ll need a web server such as Apache or Nginx. Start by pushing your project to a GitHub repository. Services like Laravel Forge and DigitalOcean make deployment easy with a few setup steps. Ensure that you set the environment variables correctly in production for database access and any other necessary configurations.
Conclusion
Building a blog with Laravel 11 covers a range of essential skills, from setting up a new Laravel project to deploying it live. By following this guide, you should have a working blog application ready to expand as you grow your Laravel knowledge.
Was this helpful?
Share this post
Comments (0)
Want to join the conversation?
Log in or sign up to leave a comment and share your thoughts.
Log in to Comment