PHP e-commerce system product management module guide: create database tables, define models, create controllers, design views, and add and modify product information.
PHP E-commerce System Development Guide: Product Management
1. Database design
Before building the product management module, a database table must be created to store product information. The structure of the table can be as follows:
CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, description TEXT, price DECIMAL(10,2) NOT NULL, quantity INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
2. Model definition
Create a Product
model to represent the product table data:
class Product extends Model { protected $table = 'products'; protected $fillable = ['name', 'description', 'price', 'quantity']; }
3. Controller
Create ProductsController
to handle product-related requests:
class ProductsController extends Controller { public function index() { $products = Product::all(); return view('products.index', compact('products')); } public function create() { return view('products.create'); } public function store(Request $request) { $product = new Product; $product->name = $request->input('name'); $product->description = $request->input('description'); $product->price = $request->input('price'); $product->quantity = $request->input('quantity'); $product->save(); return redirect()->route('products.index'); } // ... 其余方法 }
4. View
Createindex.blade.php
View is used to display the product list:
@extends('layouts.app') @section('content') <h1>Products</h1> <table border="1"> <tr> <th>ID</th> <th>Name</th> <th>Description</th> <th>Price</th> <th>Quantity</th> </tr> @foreach ($products as $product) <tr> <td>{{ $product->id }}</td> <td>{{ $product->name }}</td> <td>{{ $product->description }}</td> <td>{{ $product->price }}</td> <td>{{ $product->quantity }}</td> </tr> @endforeach </table> @endsection
Practical case
Add New Product
/products/create
to create a new product. Modify existing products
/products/{product_id}/edit
to modify existing products. The above is the detailed content of PHP e-commerce system development guide product management. For more information, please follow other related articles on the PHP Chinese website!