Laravel Framework

Intermediate Laravel Application: Blog Platform

Published · Updated 5 min read
Knowledge base article
Contents (25 sections)

Blogs have grown from simple online journals to powerful tools for communication, marketing, and community building. With Laravel, constructing a feature-rich blog platform becomes a straightforward task, blending creativity with efficient coding. Let's build this!


1. Project Setup and Initial Configuration:

a. Setting up Laravel on DomainIndia.com cPanel hosting:

Follow the instructions provided in this guide to set up Laravel on DomainIndia's cPanel hosting.

b. Database Configuration:

Create a new database in your cPanel, and then update the .env file with your database credentials:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

2. Data Structure and Database Setup:

a. Models and Migrations:

Start by creating the necessary models and migrations:

php artisan make:model BlogPost -m
php artisan make:model Category -m
php artisan make:model Tag -m
php artisan make:model Comment -m

Define your database structure in the migrations:

  • BlogPost: id, title, content, user_id, category_id, timestamps.
  • Category: id, name, timestamps.
  • Tag: id, name, timestamps.
  • Comment: id, blog_post_id, user_id, content, timestamps.

And set up many-to-many relationships between BlogPosts and Tags.

b. Eloquent Relationships:

In the BlogPost model, you might have:

public function category()
{
return $this->belongsTo(Category::class);
}

public function comments()
{
return $this->hasMany(Comment::class);
}

public function tags()
{
return $this->belongsToMany(Tag::class);
}

3. Controllers and Routes:

a. Controllers:

Generate controllers for CRUD operations:

php artisan make:controller BlogPostController --resource
php artisan make:controller CategoryController --resource
php artisan make:controller TagController --resource
php artisan make:controller CommentController --resource

Fill these controllers with methods for creating, reading, updating, and deleting the respective entities.

b. Routing:

Define your routes in routes/web.php:

Route::resource('blog-posts', 'BlogPostController');
Route::resource('categories', 'CategoryController');
Route::resource('tags', 'TagController');
Route::resource('comments', 'CommentController');

4. Blade Views and Layouts:

a. Blog Post Views:

For the BlogPost model, you might have the following views:

  • index.blade.php: Lists all blog posts.
  • show.blade.php: Displays a single blog post with comments.
  • create.blade.php & edit.blade.php: Forms for creating and editing blog posts.

b. Category and Tag Management:

Provide interfaces where users can add, edit, or remove categories and tags.

c. Commenting System:

On each blog post, add a form for users to submit comments. Display comments in a nested or linear fashion, as per preference.

5. Authentication & Authorization:

a. Authentication:

Leverage Laravel's built-in authentication scaffolding:

composer require laravel/ui
php artisan ui bootstrap --auth

b. Authorization:

Ensure that only authorized users can create, edit, or delete blog posts, categories, tags, or comments. Laravel's policies and gates make this task simpler.

6. Example: Displaying a Blog Post:

In BlogPostController:

public function show(BlogPost $blogPost)
{
return view('blog-posts.show', compact('blogPost'));
}

<h2>{{ $blogPost->title }}</h2>
<p>{{ $blogPost->content }}</p>
@foreach($blogPost->comments as $comment)
<div>{{ $comment->content }}</div>
@endforeach

Constructing a blog platform in Laravel is a combination of efficient database design, seamless UI/UX, and secure operations. By adhering to best practices and utilizing Laravel's rich ecosystem, building a blog becomes not just easy but enjoyable.

7. Tags and Categories:

a. Tag Management:

Each blog post can be associated with multiple tags, allowing for effective categorization and search.

In BlogPostController:

public function create()
{
$tags = Tag::all();
return view('blog-posts.create', compact('tags'));
}

For associating tags with a post:

public function store(Request $request)
{
$blogPost = new BlogPost($request->all());
$blogPost->save();

$blogPost->tags()->sync($request->tags);

return redirect('blog-posts');
}

In create.blade.php, you can use checkboxes for tag selection:

@foreach($tags as $tag)
<input type="checkbox" name="tags[]" value="{{ $tag->id }}"> {{ $tag->name }}<br>
@endforeach

b. Category Management:

Each post can belong to a category. This allows for broader classifications like 'Technology', 'Travel', etc.

For associating a post with a category:

public function store(Request $request)
{
$blogPost = new BlogPost($request->all());
$blogPost->category_id = $request->category;
$blogPost->save();
...
}

In create.blade.php, use a dropdown for category selection:

<select name="category">
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>

8. Comment System:

a. Comment Creation:

Once a reader views a post, they can leave comments.

In CommentController:

php

public function store(Request $request, BlogPost $blogPost)
{
$comment = new Comment($request->all());
$comment->blog_post_id = $blogPost->id;
$comment->user_id = auth()->id();
$comment->save();

return back();
}

In show.blade.php, below the post content:

<form action="{{ route('comments.store', $blogPost->id) }}" method="post">
@csrf
<textarea name="content"></textarea>
<button type="submit">Post Comment</button>
</form>

b. Displaying Comments:

For nested comments or replies, consider using a package like Laravel Comments.


9. Search Functionality:

Allow users to search posts by title or content.

In BlogPostController:

public function index(Request $request)
{
$query = $request->input('query');
$posts = BlogPost::where('title', 'LIKE', "%$query%")
->orWhere('content', 'LIKE', "%$query%")
->get();
return view('blog-posts.index', compact('posts'));
}

In your blog listing view:

<form action="{{ route('blog-posts.index') }}" method="get">
<input type="text" name="query" placeholder="Search...">
<button type="submit">Search</button>
</form>

10. Final Touches and Enhancements:

  • Pagination: Use Laravel's built-in pagination for blog posts listing: $posts = BlogPost::paginate(10);

  • Rich Text: Consider integrating tools like TinyMCE or Quill for rich text editing.

  • Image Uploads: If your posts will contain images, use packages like Intervention Image for easy image handling.


Constructing a comprehensive blog platform requires meticulous attention to detail and a user-centric approach. With Laravel's robust features, the task becomes both systematic and efficient. Happy coding!

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app
Intermediate Laravel Application: Blog Platform - Knowledge Base