A complete e-commerce platform built with Laravel, featuring product management, shopping cart, order processing, and Stripe payment integration.
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
# 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
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);
}
}
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;
}
}
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'));
}
}
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',
];
}
}
# Run tests
php artisan test
# Specific tests
php artisan test --filter=ProductTest
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');
}
}
# 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
# 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
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']);
GET /api/products
- List productsGET /api/products/{id}
- Get product detailsPOST /api/cart/add
- Add to cartGET /api/cart
- Get cart contentsPOST /api/checkout
- Process checkoutGET /api/orders
- List ordersgit 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:
E-commerce Shop - Complete Laravel e-commerce platform with payment integration ๐