laravel-from-zero-to-production

E-commerce Shop - Laravel

A complete e-commerce platform built with Laravel, featuring product management, shopping cart, order processing, and Stripe payment integration.

๐Ÿš€ Features

โœ… Product Management

โœ… Shopping Experience

โœ… Order Management

โœ… User Management

โœ… Admin Features

๐Ÿ“ Project Structure

shop-app/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ Http/Controllers/
โ”‚   โ”‚   โ”œโ”€โ”€ ProductController.php    # Product management
โ”‚   โ”‚   โ”œโ”€โ”€ CartController.php       # Shopping cart
โ”‚   โ”‚   โ”œโ”€โ”€ OrderController.php      # Order processing
โ”‚   โ”‚   โ””โ”€โ”€ CheckoutController.php   # Checkout process
โ”‚   โ”œโ”€โ”€ Models/
โ”‚   โ”‚   โ”œโ”€โ”€ Product.php             # Product model
โ”‚   โ”‚   โ”œโ”€โ”€ Order.php               # Order model
โ”‚   โ”‚   โ”œโ”€โ”€ Cart.php                # Cart model
โ”‚   โ”‚   โ””โ”€โ”€ User.php                # User model
โ”‚   โ”œโ”€โ”€ Services/
โ”‚   โ”‚   โ”œโ”€โ”€ CartService.php         # Cart business logic
โ”‚   โ”‚   โ”œโ”€โ”€ OrderService.php        # Order processing
โ”‚   โ”‚   โ””โ”€โ”€ PaymentService.php      # Payment handling
โ”‚   โ””โ”€โ”€ Policies/
โ”‚       โ””โ”€โ”€ OrderPolicy.php         # Authorization policies
โ”œโ”€โ”€ database/
โ”‚   โ””โ”€โ”€ migrations/
โ”‚       โ”œโ”€โ”€ create_products_table.php
โ”‚       โ”œโ”€โ”€ create_orders_table.php
โ”‚       โ””โ”€โ”€ create_cart_items_table.php
โ”œโ”€โ”€ resources/
โ”‚   โ””โ”€โ”€ views/
โ”‚       โ”œโ”€โ”€ products/
โ”‚       โ”‚   โ”œโ”€โ”€ index.blade.php     # Product listing
โ”‚       โ”‚   โ”œโ”€โ”€ show.blade.php      # Product details
โ”‚       โ”‚   โ””โ”€โ”€ search.blade.php    # Search results
โ”‚       โ”œโ”€โ”€ cart/
โ”‚       โ”‚   โ”œโ”€โ”€ index.blade.php     # Shopping cart
โ”‚       โ”‚   โ””โ”€โ”€ checkout.blade.php  # Checkout form
โ”‚       โ”œโ”€โ”€ orders/
โ”‚       โ”‚   โ”œโ”€โ”€ index.blade.php     # Order history
โ”‚       โ”‚   โ””โ”€โ”€ show.blade.php      # Order details
โ”‚       โ””โ”€โ”€ admin/
โ”‚           โ””โ”€โ”€ dashboard.blade.php # Admin panel
โ”œโ”€โ”€ routes/
โ”‚   โ””โ”€โ”€ web.php                    # Application routes
โ””โ”€โ”€ README.md                      # This file

๐Ÿ› ๏ธ Installation

1. Prerequisites

2. Installation

# Clone the project
git clone <repository-url>
cd shop-app

# 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=shop_app
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

# Configure Stripe (optional for testing)
STRIPE_KEY=pk_test_your_public_key
STRIPE_SECRET=sk_test_your_secret_key

# Start server
php artisan serve

3. Access the application

๐Ÿ“š Laravel Concepts Used

Eloquent Models with Relationships

class Product extends Model
{
    protected $fillable = [
        'name', 'slug', 'description', 'price', 'sale_price',
        'stock_quantity', 'sku', 'images', 'category_id', 'brand_id'
    ];

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

    public function brand()
    {
        return $this->belongsTo(Brand::class);
    }

    public function orders()
    {
        return $this->belongsToMany(Order::class, 'order_items')
                    ->withPivot('quantity', 'price');
    }

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('active', true);
    }

    public function scopeInStock($query)
    {
        return $query->where('stock_quantity', '>', 0);
    }
}

Service Classes

class CartService
{
    public function addToCart($productId, $quantity = 1)
    {
        $cart = session()->get('cart', []);
        
        if (isset($cart[$productId])) {
            $cart[$productId]['quantity'] += $quantity;
        } else {
            $product = Product::find($productId);
            $cart[$productId] = [
                'name' => $product->name,
                'price' => $product->price,
                'quantity' => $quantity,
                'image' => $product->featured_image
            ];
        }
        
        session()->put('cart', $cart);
    }

    public function getCartTotal()
    {
        $cart = session()->get('cart', []);
        $total = 0;
        
        foreach ($cart as $item) {
            $total += $item['price'] * $item['quantity'];
        }
        
        return $total;
    }
}

Resource Controllers

class ProductController extends Controller
{
    public function index(Request $request): View
    {
        $query = Product::with(['category', 'brand'])->active();

        // Search
        if ($request->has('search')) {
            $query->where('name', 'like', '%' . $request->search . '%');
        }

        // Filter by category
        if ($request->has('category')) {
            $query->where('category_id', $request->category);
        }

        $products = $query->paginate(12);
        $categories = Category::all();

        return view('products.index', compact('products', 'categories'));
    }

    public function show(Product $product): View
    {
        $product->load(['category', 'brand', 'reviews.user']);
        $relatedProducts = Product::where('category_id', $product->category_id)
                                 ->where('id', '!=', $product->id)
                                 ->limit(4)
                                 ->get();

        return view('products.show', compact('product', 'relatedProducts'));
    }
}

Form Requests for Validation

class StoreOrderRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'shipping_address' => 'required|array',
            'shipping_address.name' => 'required|string|max:255',
            'shipping_address.email' => 'required|email',
            'shipping_address.phone' => 'required|string|max:20',
            'shipping_address.address' => 'required|string',
            'shipping_address.city' => 'required|string|max:100',
            'shipping_address.postal_code' => 'required|string|max:10',
            'shipping_address.country' => 'required|string|max:100',
            'billing_address' => 'required|array',
            'payment_method' => 'required|in:stripe,paypal',
            'notes' => 'nullable|string|max:500',
        ];
    }
}

๐ŸŽฏ Detailed Features

Product Management

Shopping Cart

Checkout Process

Order Management

Admin Features

๐Ÿ”ง Customization

Add new product types

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

Customize payment methods

  1. Integrate additional payment gateways
  2. Create custom payment services
  3. Update checkout process
  4. Add payment validation

Extend functionality

๐Ÿงช Testing

Unit Tests

# Run tests
php artisan test

# Specific tests
php artisan test --filter=ProductTest

Feature Tests

class ProductTest extends TestCase
{
    use RefreshDatabase;

    public function test_can_view_product()
    {
        $product = Product::factory()->create();

        $response = $this->get("/products/{$product->slug}");

        $response->assertStatus(200)
                ->assertSee($product->name);
    }

    public function test_can_add_to_cart()
    {
        $product = Product::factory()->create();

        $response = $this->post("/cart/add", [
            'product_id' => $product->id,
            'quantity' => 2
        ]);

        $response->assertRedirect();
        $this->assertSessionHas('cart');
    }
}

๐Ÿš€ Deployment

Heroku

# Create Heroku app
heroku create shop-app-laravel

# Configure environment variables
heroku config:set APP_KEY=base64:your-key
heroku config:set DB_CONNECTION=postgresql
heroku config:set STRIPE_KEY=your-stripe-key
heroku config:set STRIPE_SECRET=your-stripe-secret

# Deploy
git push heroku main

# Run migrations
heroku run php artisan migrate

VPS/Dedicated Server

# Clone on server
git clone <repository-url>
cd shop-app

# 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 shop can be extended with a REST API:

// routes/api.php
Route::apiResource('products', ProductApiController::class);
Route::apiResource('orders', OrderApiController::class);
Route::post('cart/add', [CartApiController::class, 'add']);
Route::post('checkout', [CheckoutApiController::class, 'process']);

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

E-commerce Shop - Complete Laravel e-commerce platform with payment integration ๐Ÿ›’