Example output
Creating an e-commerce website for handmade crafts using PHP and Laravel involves several well-defined steps. Below is a comprehensive guide:
---
### **Step 1: Project Setup**
1. **Install Laravel Framework**:
```bash
composer create-project --prefer-dist laravel/laravel handmade_crafts
cd handmade_crafts
```
2. **Setup Environment**:
- Configure `.env` file with your database credentials:
```plaintext
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=handmade_crafts
DB_USERNAME=your_username
DB_PASSWORD=your_password
```
3. **Install Dependencies**:
- For UI and authentication scaffolding:
```bash
composer require laravel/ui
php artisan ui bootstrap --auth
npm install && npm run dev
```
---
### **Step 2: Structuring the Project**
Follow Laravel's MVC architecture for scalability:
- **Controllers**: Handle user requests and business logic.
- **Models**: Represent database tables (e.g., `User`, `Product`, `Order`).
- **Views**: Blade templates for the frontend.
Recommended folder structure:
```
app/
Http/
Controllers/
Auth/
Admin/
Api/
User/
Middleware/
Models/
User.php
Product.php
Order.php
Payment.php
resources/
views/
layouts/
components/
auth/
user/
admin/
routes/
web.php
api.php
```
---
### **Step 3: Database Schema Design**
Create tables for the following entities:
- **Users**:
```sql
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255) UNIQUE,
password VARCHAR(255),
role ENUM('customer', 'admin') DEFAULT 'customer',
created_at TIMESTAMP,
updated_at TIMESTAMP
);
```
- **Products**:
```sql
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
description TEXT,
price DECIMAL(10,2),
stock INT,
category VARCHAR(100),
image_url VARCHAR(255),
created_at TIMESTAMP,
updated_at TIMESTAMP
);
```
- **Orders**:
```sql
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
total DECIMAL(10,2),
status ENUM('pending', 'completed', 'cancelled') DEFAULT 'pending',
created_at TIMESTAMP,
updated_at TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
```
- **Order Items**:
```sql
CREATE TABLE order_items (
id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT,
product_id INT,
quantity INT,
price DECIMAL(10,2),
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (product_id) REFERENCES products(id)
);
```
- **Payments**:
```sql
CREATE TABLE payments (
id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT,
payment_method VARCHAR(50),
payment_status ENUM('pending', 'paid', 'failed') DEFAULT 'pending',
transaction_id VARCHAR(255),
created_at TIMESTAMP,
updated_at TIMESTAMP,
FOREIGN KEY (order_id) REFERENCES orders(id)
);
```
---
### **Step 4: Implementing User Authentication**
1. **Authentication**:
Laravel provides built-in authentication:
```bash
php artisan make:auth
```
2. **Authorization**:
- Add middleware for role-based access control:
```php
Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/admin', [AdminController::class, 'index']);
});
```
3. **Customize User Roles**:
- Add a `role` field in the `users` table to distinguish between customers and admins.
---
### **Step 5: Developing APIs**
Use `routes/api.php` for RESTful APIs.
1. **Product Management**:
```php
Route::middleware('auth:api')->group(function () {
Route::get('/products', [ProductController::class, 'index']);
Route::post('/products', [ProductController::class, 'store']);
Route::put('/products/{id}', [ProductController::class, 'update']);
Route::delete('/products/{id}', [ProductController::class, 'destroy']);
});
```
2. **Order Processing**:
```php
Route::post('/orders', [OrderController::class, 'store']);
Route::get('/orders', [OrderController::class, 'index']);
```
3. **Inventory Management**:
Automatically decrement product stock after a successful purchase:
```php
$product->decrement('stock', $quantity);
```
---
### **Step 6: Adding Features**
1. **Search and Filters**:
```php
Route::get('/products/search', [ProductController::class, 'search']);
```
Implement filter logic in `ProductController`:
```php
public function search(Request $request) {
$products = Product::query();
if ($request->has('category')) {
$products->where('category', $request->input('category'));
}
if ($request->has('price_min')) {
$products->where('price', '>=', $request->input('price_min'));
}
if ($request->has('price_max')) {
$products->where('price', '<=', $request->input('price_max'));
}
return response()->json($products->get());
}
```
2. **Product Reviews**:
- Add a `reviews` table with `user_id`, `product_id`, `rating`, and `comment`.
---
### **Step 7: Integrating Payment Gateway**
1. **Install Stripe or PayPal SDK**:
```bash
composer require stripe/stripe-php
```
2. **Implement Payment Flow**:
- Use Stripe:
```php
Stripe::setApiKey(env('STRIPE_SECRET'));
$paymentIntent = PaymentIntent::create([
'amount' => $order->total * 100, // in cents
'currency' => 'usd',
]);
```
- Confirm payment:
```php
$order->payment_status = 'paid';
$order->save();
```
---
### **Step 8: Responsive Design**
1. Use **Bootstrap** for responsiveness:
```bash
npm install bootstrap
```
2. Add a responsive navigation bar and grid layouts for product listings.
---
### **Step 9: Optimization**
1. **Database Indexing**:
- Add indexes to frequently queried fields like `email`, `product_name`, and `category`.
2. **Caching**:
- Use Laravel's caching system for queries:
```php
$products = Cache::remember('products', 3600, function () {
return Product::all();
});
```
3. **Image Optimization**:
- Use libraries like `Intervention/Image` to compress images:
```bash
composer require intervention/image
```
---
### **Step 10: Comments and Documentation**
Comment your code thoroughly:
```php
// Retrieve all products with pagination
public function index() {
return Product::paginate(10);
}
```
Use API documentation tools like **Swagger** to generate API docs.
---
### **Step 11: Deploy the Application**
1. Use **Forge** or similar tools to deploy.
2. Configure your `.env` for production:
```plaintext
APP_ENV=production
APP_DEBUG=false
```
---
Would you like sample code snippets for specific parts of this guide?