0% found this document useful (0 votes)
16 views6 pages

A Simple Blogging System built with Laravel

This document outlines the steps to create a simple blogging system using Laravel, including CRUD operations for managing blog posts. It details prerequisites, installation, database setup, model and controller creation, route definitions, and view implementations. The system features basic form validation, Bootstrap styling, and user feedback through flash messages.

Uploaded by

SAAD TRA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

A Simple Blogging System built with Laravel

This document outlines the steps to create a simple blogging system using Laravel, including CRUD operations for managing blog posts. It details prerequisites, installation, database setup, model and controller creation, route definitions, and view implementations. The system features basic form validation, Bootstrap styling, and user feedback through flash messages.

Uploaded by

SAAD TRA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

A Simple Blogging System built with Laravel, implementing CRUD (Create, Read,

Update, Delete) operations for managing blog posts.

Prerequisites

Ensure you have the following installed:

• PHP (>= 8.0)


• Composer
• Laravel (Latest version)
• MySQL or SQLite for database

Step 1: Install Laravel and Set Up Project


composer create-project --prefer-dist laravel/laravel blog-system
cd blog-system

Step 2: Set Up Database


Edit .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog_system
DB_USERNAME=root
DB_PASSWORD=yourpassword

Then run:

php artisan migrate

Step 3: Create Model, Migration, and Controller


Generate the Post model and migration:

php artisan make:model Post -mcr

Now, edit the generated migration file


(database/migrations/xxxx_xx_xx_xxxxxx_create_posts_table.php):

public function up() {


Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title')->unique();
$table->text('description');
$table->timestamps();
});
}
Run migrations:

php artisan migrate

Step 4: Define Routes


Edit routes/web.php:

use App\Http\Controllers\PostController;

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


Route::resource('posts', PostController::class);

Step 5: Implement PostController


Edit app/Http/Controllers/PostController.php:

namespace App\Http\Controllers;

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

class PostController extends Controller


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

public function create()


{
return view('posts.create');
}

public function store(Request $request)


{
$request->validate([
'title' => 'required|min:3|unique:posts',
'description' => 'required|min:10',
]);

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

public function show(Post $post)


{
return view('posts.show', compact('post'));
}

public function edit(Post $post)


{
return view('posts.edit', compact('post'));
}

public function update(Request $request, Post $post)


{
$request->validate([
'title' => 'required|min:3|unique:posts,title,' . $post->id,
'description' => 'required|min:10',
]);

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

public function destroy(Post $post)


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

Step 6: Create Views


Inside resources/views/posts/, create the following Blade files:

1. Layout (layout.blade.php)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Blog</title>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.c
ss">
</head>
<body>
<div class="container mt-4">
@yield('content')
</div>
</body>
</html>

2. List Posts (index.blade.php)


@extends('layout')

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

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

<table class="table">
<tr>
<th>Title</th>
<th>Actions</th>
</tr>
@foreach ($posts as $post)
<tr>
<td>{{ $post->title }}</td>
<td>
<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" style="display:inline;">
@csrf @method('DELETE')
<button type="submit" class="btn btn-danger btn-
sm">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
@endsection

3. Create Post (create.blade.php)


@extends('layout')

@section('content')
<h2>Create Post</h2>

@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

<form action="{{ route('posts.store') }}" method="POST">


@csrf
<div class="mb-3">
<label>Title:</label>
<input type="text" name="title" class="form-control" required>
</div>
<div class="mb-3">
<label>Description:</label>
<textarea name="description" class="form-control"
required></textarea>
</div>
<button type="submit" class="btn btn-success">Save</button>
</form>
@endsection
4. Edit Post (edit.blade.php)
@extends('layout')

@section('content')
<h2>Edit Post</h2>

<form action="{{ route('posts.update', $post) }}" method="POST">


@csrf @method('PUT')
<div class="mb-3">
<label>Title:</label>
<input type="text" name="title" class="form-control" value="{{
$post->title }}" required>
</div>
<div class="mb-3">
<label>Description:</label>
<textarea name="description" class="form-control" required>{{
$post->description }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection

5. Show Post (show.blade.php)


@extends('layout')

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

Step 7: Run the Application


php artisan serve

Visit http://127.0.0.1:8000/ in your browser.

Features Implemented
CRUD operations (Create, Read, Update, Delete)
Basic form validation
Laravel Blade templating with Bootstrap styling
Flash messages for user feedback

This is a simple yet fully functional Laravel blogging system. Let me know if you need
additional features like authentication, image uploads, or comments!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy