laravel-from-zero-to-production

Blog Platform - Laravel

A complete blog platform built with Laravel, featuring authentication, content management, and user interactions.

๐Ÿš€ Features

โœ… Content Management

โœ… User Management

โœ… Content Features

โœ… Advanced Features

๐Ÿ“ Project Structure

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

๐Ÿ› ๏ธ Installation

1. Prerequisites

2. Installation

# 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

3. Access the application

๐Ÿ“š Laravel Concepts Used

Eloquent Models with Relationships

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());
    }
}

Resource Controllers

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!');
    }
}

Form Requests for Validation

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',
        ];
    }
}

Authorization Policies

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();
    }
}

๐ŸŽฏ Detailed Features

Content Management

User System

Comment System

SEO and Performance

๐Ÿ”ง Customization

Add new content types

  1. Create new models and migrations
  2. Add relationships to existing models
  3. Create controllers and views
  4. Update routes and policies

Custom themes

  1. Create new Blade layouts
  2. Customize CSS and JavaScript
  3. Add theme configuration options
  4. Implement theme switching

Extend functionality

๐Ÿงช Testing

Unit Tests

# Run tests
php artisan test

# Specific tests
php artisan test --filter=PostTest

Feature Tests

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']);
    }
}

๐Ÿš€ Deployment

Heroku

# 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

VPS/Dedicated Server

# 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

๐Ÿ“ API Endpoints

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']);

Available endpoints

๐Ÿค Contributing

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

๐Ÿ†˜ Support

For any questions or issues:

  1. Check this README
  2. Consult Laravel documentation
  3. Open an issue on GitHub

Blog Platform - Complete Laravel blog platform with advanced features ๐Ÿ“