A complete blog platform built with Laravel, featuring authentication, content management, and user interactions.
blog-platform/
โโโ app/
โ โโโ Http/Controllers/
โ โ โโโ PostController.php # Post management
โ โ โโโ CommentController.php # Comment handling
โ โโโ Models/
โ โ โโโ Post.php # Post model
โ โ โโโ User.php # User model
โ โ โโโ Comment.php # Comment model
โ โโโ Policies/
โ โโโ PostPolicy.php # Authorization policies
โโโ database/
โ โโโ migrations/
โ โโโ create_posts_table.php
โ โโโ create_comments_table.php
โ โโโ create_categories_table.php
โโโ resources/
โ โโโ views/
โ โโโ posts/
โ โ โโโ index.blade.php # Post listing
โ โ โโโ show.blade.php # Single post view
โ โ โโโ create.blade.php # Create form
โ โ โโโ edit.blade.php # Edit form
โ โโโ admin/
โ โ โโโ dashboard.blade.php # Admin panel
โ โโโ layouts/
โ โโโ app.blade.php # Main layout
โโโ routes/
โ โโโ web.php # Application routes
โโโ README.md # This file
# Clone the project
git clone <repository-url>
cd blog-platform
# Install dependencies
composer install
# Copy environment file
cp .env.example .env
# Generate application key
php artisan key:generate
# Configure database in .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog_platform
DB_USERNAME=root
DB_PASSWORD=
# Run migrations
php artisan migrate
# Install Laravel Breeze
composer require laravel/breeze --dev
php artisan breeze:install blade
# Install frontend assets
npm install
npm run build
# Create storage link
php artisan storage:link
# Start server
php artisan serve
class Post extends Model
{
protected $fillable = [
'title', 'slug', 'content', 'featured_image',
'published_at', 'status', 'user_id', 'category_id'
];
// Relationships
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
// Scopes
public function scopePublished($query)
{
return $query->where('status', 'published')
->where('published_at', '<=', now());
}
}
class PostController extends Controller
{
public function index(): View
{
$posts = Post::with(['user', 'category'])
->published()
->latest('published_at')
->paginate(12);
return view('posts.index', compact('posts'));
}
public function store(StorePostRequest $request): RedirectResponse
{
$validated = $request->validated();
if ($request->hasFile('featured_image')) {
$validated['featured_image'] = $request->file('featured_image')
->store('posts', 'public');
}
$post = auth()->user()->posts()->create($validated);
return redirect()->route('posts.show', $post)
->with('success', 'Post created successfully!');
}
}
class StorePostRequest extends FormRequest
{
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'content' => 'required|string|min:100',
'category_id' => 'required|exists:categories,id',
'featured_image' => 'nullable|image|max:2048',
'status' => 'required|in:draft,published',
'published_at' => 'nullable|date|after:now',
];
}
}
class PostPolicy
{
public function update(User $user, Post $post): bool
{
return $user->id === $post->user_id || $user->isAdmin();
}
public function delete(User $user, Post $post): bool
{
return $user->id === $post->user_id || $user->isAdmin();
}
}
# Run tests
php artisan test
# Specific tests
php artisan test --filter=PostTest
class PostTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_create_post()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post('/posts', [
'title' => 'Test Post',
'content' => 'This is a test post content.',
'category_id' => 1,
'status' => 'published'
]);
$response->assertRedirect();
$this->assertDatabaseHas('posts', ['title' => 'Test Post']);
}
}
# Create Heroku app
heroku create blog-platform-laravel
# Configure environment variables
heroku config:set APP_KEY=base64:your-key
heroku config:set DB_CONNECTION=postgresql
heroku config:set FILESYSTEM_DISK=s3
# Deploy
git push heroku main
# Run migrations
heroku run php artisan migrate
# Clone on server
git clone <repository-url>
cd blog-platform
# Install dependencies
composer install --optimize-autoloader --no-dev
npm install && npm run build
# Configure environment
cp .env.example .env
php artisan key:generate
# Run migrations
php artisan migrate
# Optimize for production
php artisan config:cache
php artisan route:cache
php artisan view:cache
The platform can be extended with a REST API:
// routes/api.php
Route::apiResource('posts', PostApiController::class);
Route::apiResource('comments', CommentApiController::class);
Route::post('posts/{post}/like', [PostApiController::class, 'like']);
GET /api/posts
- List postsPOST /api/posts
- Create postGET /api/posts/{id}
- Get post detailsPUT /api/posts/{id}
- Update postDELETE /api/posts/{id}
- Delete postPOST /api/posts/{id}/comments
- Add commentgit checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)This project is licensed under the MIT License. See the LICENSE
file for details.
For any questions or issues:
Blog Platform - Complete Laravel blog platform with advanced features ๐