FahmidasClassroom

Learn by easy steps

P8

A beginner-level Laravel project, a simple blog application has been shown in this tutorial that will help you understand key Laravel concepts like routing, controllers, Blade templates, models, migrations, and CRUD operations.

📝 Laravel Beginner Project: Simple Blog App

🎯 Goal of this tutorial:

Create a basic blog app that lets users:

  • View all posts
  • View a single post
  • Create a new post
  • Edit a post
  • Delete a post

✅ Step 1: Create a New Laravel Project

composer create-project laravel/laravel newapp
cd newapp
php artisan serve
P1

Visit http://localhost:8000 to verify it works.

✅ Step 2: Configure Database

Edit .env File: set the necessary database configuration.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=newapp
DB_USERNAME=root
DB_PASSWORD=your_password

After editing the .env file will be looked like the following image.

P2

Create Database:

Create a new database named newapp by using mysql command or phpMyAdmin:

CREATE DATABASE newapp;

The phpMyAdmin will look like the following image after creating the database.

P3

✅ Step 3: Create Post Model, Migration, and Controller

Run the following command to create model, controller, and migration files.

php artisan make:model Post -mcr
P4

This creates:

  • Post.php (Model)
  • 202x_xx_xx_create_posts_table.php (Migration)
  • PostController.php (Controller)
  • Registers a resource route

✅ Step 4: Define Posts Table Schema

Open database/migrations/xxxx_create_posts_table.php and update the up() function with the following content.

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('body');
        $table->timestamps();
    });
}

Run the migration command to create the required database tables.

php artisan migrate

✅ Step 5: Set Up Routes

In routes/web.php, add the following content.

use App\Http\Controllers\PostController;

Route::get('/', [PostController::class, 'index']);
Route::resource('posts', PostController::class);

✅ Step 6: Build PostController Methods

Open app/Http/Controllers/PostController.php and update the file with the following content.

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::latest()->get();
        return view('index', compact('posts'));
    }

    public function create()
    {
        return view('create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required',
            'body' => 'required'
        ]);

        Post::create($request->all());
        return redirect()->route('posts.index')->with('success', 'Post created!');
    }

    public function show(Post $post)
    {
        return view('show', compact('post'));
    }

    public function edit(Post $post)
    {
        return view('edit', compact('post'));
    }

    public function update(Request $request, Post $post)
    {
        $request->validate([
            'title' => 'required',
            'body' => 'required'
        ]);

        $post->update($request->all());
        return redirect()->route('posts.index')->with('success', 'Post updated!');
    }

    public function destroy(Post $post)
    {
        $post->delete();
        return redirect()->route('posts.index')->with('success', 'Post deleted!');
    }
}

Also, enable mass assignment in the model:

In app/Models/Post.php add the following content.

protected $fillable = ['title', 'body'];

✅ Step 7: Create Blade Templates

Add the following content in the resources/views/layout.blade.php file.

<!DOCTYPE html>
<html>
<head>
    <title>Simple Blog</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="container mt-4">
    <h2>Simple Laravel Blog</h2>
    <hr>
    @yield('content')
</body>
</html>

Add the following content in the resources/views/posts/index.blade.php file to display the index page.

@extends('layout')

@section('content')
<a href="{{ route('posts.create') }}" class="btn btn-primary mb-3">Create New Post</a>

@if(session('success'))
    <div class="alert alert-success">{{ session('success') }}</div>
@endif

@foreach($posts as $post)
    <div class="card mb-3">
        <div class="card-body">
            <h4>{{ $post->title }}</h4>
            <p>{{ Str::limit($post->body, 150) }}</p>
            <a href="{{ route('posts.show', $post) }}" class="btn btn-info btn-sm">View</a>
            <a href="{{ route('posts.edit', $post) }}" class="btn btn-warning btn-sm">Edit</a>
            <form action="{{ route('posts.destroy', $post) }}" method="POST" class="d-inline">
                @csrf @method('DELETE')
                <button class="btn btn-danger btn-sm" onclick="return confirm('Delete post?')">Delete</button>
            </form>
        </div>
    </div>
@endforeach
@endsection

Add the following content in the resources/views/posts/create.blade.php file to create the post.

@extends('layout')

@section('content')
<h3>Create New Post</h3>
<form action="{{ route('posts.store') }}" method="POST">
    @csrf
    <div class="mb-3">
        <label>Title</label>
        <input name="title" class="form-control" required>
    </div>
    <div class="mb-3">
        <label>Body</label>
        <textarea name="body" class="form-control" required></textarea>
    </div>
    <button class="btn btn-success">Submit</button>
</form>
@endsection

Add the following content in the resources/views/posts/show.blade.php file to view the post.

@extends('layout')

@section('content')
<h3>{{ $post->title }}</h3>
<p>{{ $post->body }}</p>
<a href="{{ route('posts.index') }}" class="btn btn-secondary">Back</a>
@endsection

Add the following content in the resources/views/posts/edit.blade.php file to edit the post.

@extends('layout')

@section('content')
<h3>Edit Post</h3>
<form action="{{ route('posts.update', $post) }}" method="POST">
    @csrf @method('PUT')
    <div class="mb-3">
        <label>Title</label>
        <input name="title" value="{{ $post->title }}" class="form-control" required>
    </div>
    <div class="mb-3">
        <label>Body</label>
        <textarea name="body" class="form-control" required>{{ $post->body }}</textarea>
    </div>
    <button class="btn btn-primary">Update</button>
</form>
@endsection

Now, restart the Laravel server. The following page will appear.

P6

Click on the Create New Post button to open the new post form.

P7

Click on the Submit button after adding the title and body content. The following page will appear if the post is added in the database properly.

P8

You can now view, edit, and delete posts using this app.