DIPANDU SAMPAI JADI | CARA MEMBUAT BLOG MENGGUNAKAN LARAVEL 11

4 min read 2 hours ago
Published on Oct 08, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

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

  1. Check Requirements

    • Ensure you have PHP 8.1 or higher installed.
    • Install Composer for managing Laravel dependencies.
  2. Create a New Laravel Project

    • Open your terminal and run the following command:
      composer create-project laravel/laravel blog
      

Step 2: Set Up the Database

  1. Create a MySQL Database

    • Log in to your MySQL server and create a new database for your blog:
      CREATE DATABASE blog_db;
      
  2. 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
      

Step 3: Install Laravel Breeze for Authentication

  1. Install Laravel Breeze

    • Run the following command to install Breeze:
      composer require laravel/breeze --dev
      
  2. Set Up Breeze

    • Publish Breeze’s assets:
      php artisan breeze:install
      
  3. Run Migrations

    • Execute the database migrations:
      php artisan migrate
      
  4. Install Node Modules

    • Install frontend dependencies:
      npm install
      npm run dev
      

Step 4: Create a Model for Posts

  1. Generate the Post Model

    • Run the following command to create the model and migration:
      php artisan make:model Post -m
      
  2. Define the Database Schema

    • Open the migration file in database/migrations and define the posts table schema:
      Schema::create('posts', function (Blueprint $table) {
          $table->id();
          $table->string('title');
          $table->text('content');
          $table->string('slug')->unique();
          $table->timestamps();
      });
      
  3. Run the Migration

    • Execute:
      php artisan migrate
      

Step 5: Create a Controller for Blog Functionality

  1. Generate the BlogController

    • Use the following command:
      php artisan make:controller BlogController
      
  2. Define Methods in BlogController

    • Implement methods for CRUD operations (Create, Read, Update, Delete) within the controller.

Step 6: Set Up Views with Blade Templates

  1. Create Blade Views

    • Create views for listing posts, displaying a single post, and forms for creating and editing posts in the resources/views directory.
  2. Display Posts Using Blade

    • Use Blade syntax to display posts:
      @foreach ($posts as $post)
          <h2>{{ $post->title }}</h2>
          <p>{{ $post->content }}</p>
      @endforeach
      

Step 7: Implement Pagination

  1. Paginate Posts

    • Modify the controller to paginate the posts:
      $posts = Post::paginate(10);
      
  2. Add Pagination Links in Your Blade View

    • Use the following syntax to show pagination links:
      {{ $posts->links() }}
      

Step 8: Create Forms for Editing and Adding Posts

  1. 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>
      
  2. Form Validation

    • Implement validation in your controller to ensure data integrity.

Step 9: Enable Image Uploads

  1. Set Up File Storage

    • Configure file storage in the .env file:
      FILESYSTEM_DRIVER=public
      
  2. 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');
      }
      

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.