DIPANDU SAMPAI JADI | CARA MEMBUAT BLOG MENGGUNAKAN LARAVEL 11
Table of Contents
Introduction
In this tutorial, we will learn how to create a blog using Laravel 11 and MySQL. We will also implement user authentication with Laravel Breeze, making the process quick and straightforward. This guide is perfect for beginners who want to build a blog from scratch, covering everything from database setup to creating models, controllers, and views.
Step 1: Install Laravel
-
Check Requirements
- Ensure you have PHP 8.1 or higher installed.
- Install Composer for managing Laravel dependencies.
-
Create a New Laravel Project
- Open your terminal and run the following command:
composer create-project laravel/laravel blog
- Open your terminal and run the following command:
Step 2: Set Up the Database
-
Create a MySQL Database
- Log in to your MySQL server and create a new database for your blog:
CREATE DATABASE blog_db;
- Log in to your MySQL server and create a new database for your blog:
-
Configure Environment Variables
- Open the
.env
file in your Laravel project and set your database connection:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=blog_db DB_USERNAME=your_username DB_PASSWORD=your_password
- Open the
Step 3: Install Laravel Breeze for Authentication
-
Install Laravel Breeze
- Run the following command to install Breeze:
composer require laravel/breeze --dev
- Run the following command to install Breeze:
-
Set Up Breeze
- Publish Breeze’s assets:
php artisan breeze:install
- Publish Breeze’s assets:
-
Run Migrations
- Execute the database migrations:
php artisan migrate
- Execute the database migrations:
-
Install Node Modules
- Install frontend dependencies:
npm install npm run dev
- Install frontend dependencies:
Step 4: Create a Model for Posts
-
Generate the Post Model
- Run the following command to create the model and migration:
php artisan make:model Post -m
- Run the following command to create the model and migration:
-
Define the Database Schema
- Open the migration file in
database/migrations
and define theposts
table schema:Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->string('slug')->unique(); $table->timestamps(); });
- Open the migration file in
-
Run the Migration
- Execute:
php artisan migrate
- Execute:
Step 5: Create a Controller for Blog Functionality
-
Generate the BlogController
- Use the following command:
php artisan make:controller BlogController
- Use the following command:
-
Define Methods in BlogController
- Implement methods for CRUD operations (Create, Read, Update, Delete) within the controller.
Step 6: Set Up Views with Blade Templates
-
Create Blade Views
- Create views for listing posts, displaying a single post, and forms for creating and editing posts in the
resources/views
directory.
- Create views for listing posts, displaying a single post, and forms for creating and editing posts in the
-
Display Posts Using Blade
- Use Blade syntax to display posts:
@foreach ($posts as $post) <h2>{{ $post->title }}</h2> <p>{{ $post->content }}</p> @endforeach
- Use Blade syntax to display posts:
Step 7: Implement Pagination
-
Paginate Posts
- Modify the controller to paginate the posts:
$posts = Post::paginate(10);
- Modify the controller to paginate the posts:
-
Add Pagination Links in Your Blade View
- Use the following syntax to show pagination links:
{{ $posts->links() }}
- Use the following syntax to show pagination links:
Step 8: Create Forms for Editing and Adding Posts
-
Create Form for Adding Posts
- In your Blade view, create a form for creating new blog posts:
<form action="{{ route('posts.store') }}" method="POST"> @csrf <input type="text" name="title" required> <textarea name="content" required></textarea> <button type="submit">Create Post</button> </form>
- In your Blade view, create a form for creating new blog posts:
-
Form Validation
- Implement validation in your controller to ensure data integrity.
Step 9: Enable Image Uploads
-
Set Up File Storage
- Configure file storage in the
.env
file:FILESYSTEM_DRIVER=public
- Configure file storage in the
-
Handle File Uploads in the Controller
- Use the following code to handle image uploads:
if ($request->hasFile('image')) { $path = $request->file('image')->store('images', 'public'); }
- Use the following code to handle image uploads:
Conclusion
Congratulations! You have now built a basic blog using Laravel 11 and MySQL. You have set up user authentication, created models and controllers, and learned how to handle CRUD operations, including image uploads and pagination. As a next step, consider enhancing your blog with features like comments, tags, or categories. Keep exploring Laravel’s extensive documentation for more advanced functionalities.