التوثيق التقني الشامل
دليل تقني مفصل لمنصة Peak Link للتجارة الإلكترونية - هيكل قاعدة البيانات، واجهات برمجية، وأمثلة عملية
نظرة عامة على المنصة
منصة Peak Link هي نظام تجارة إلكترونية متكامل مبني باستخدام Laravel 11 مع قاعدة بيانات MySQL وواجهة أمامية متجاوبة.
Laravel 11
إطار عمل PHP حديث مع أحدث المميزات والأدوات
MySQL 8.0
قاعدة بيانات قوية مع أداء عالي وموثوقية
PHP 8.1+
لغة برمجة متطورة مع مميزات حديثة
Livewire
مكونات تفاعلية بدون JavaScript معقد
هيكل المشروع
peak-link/
├── app/
│ ├── Http/Controllers/ # المتحكمات
│ ├── Models/ # النماذج
│ ├── Livewire/ # مكونات Livewire
│ ├── Services/ # الخدمات
│ └── Observers/ # المراقبات
├── database/
│ ├── migrations/ # ملفات الهجرة
│ └── seeders/ # ملفات البذر
├── resources/
│ ├── views/ # ملفات العرض
│ └── lang/ # ملفات الترجمة
└── routes/ # ملفات التوجيه
هيكل قاعدة البيانات
تحتوي قاعدة البيانات على أكثر من 100 جدول تغطي جميع جوانب التجارة الإلكترونية:
الجداول الرئيسية
العلاقات بين الجداول
// علاقة المستخدم بالطلبات
User hasMany Orders
Order belongsTo User
// علاقة المنتج بالفئات والعلامات التجارية
Product belongsTo Category
Product belongsTo Brand
Product belongsToMany Tags
// علاقة الطلب بالمنتجات
Order belongsToMany Products (pivot: order_products)
واجهات برمجة التطبيقات (API)
توفر المنصة واجهات برمجية شاملة لجميع العمليات:
المنتجات API
جلب قائمة المنتجات مع إمكانية الفلترة والبحث
{
"data": [
{
"id": 1,
"name": "هاتف ذكي Samsung",
"price": "2499.00",
"stock": 45,
"brand": {
"id": 1,
"name": "Samsung"
},
"category": {
"id": 1,
"name": "إلكترونيات"
}
}
],
"meta": {
"current_page": 1,
"total": 150
}
}
إنشاء منتج جديد
{
"name": "منتج جديد",
"name_en": "New Product",
"description_ar": "وصف المنتج",
"description_en": "Product description",
"price": 999.99,
"stock": 100,
"brand_id": 1,
"category_id": 1,
"is_featured": true
}
الطلبات API
جلب قائمة الطلبات
إنشاء طلب جديد
تحديث حالة الطلب
المستخدمين API
جلب قائمة المستخدمين
إنشاء مستخدم جديد
النماذج (Models)
النماذج الرئيسية في المنصة مع علاقاتها ووظائفها:
نموذج المنتج (Product)
class Product extends Model
{
protected $fillable = [
'name', 'name_en', 'description_ar', 'description_en',
'price', 'stock', 'brand_id', 'category_id',
'is_featured', 'is_active'
];
// العلاقات
public function brand() {
return $this->belongsTo(Brand::class);
}
public function category() {
return $this->belongsTo(Category::class);
}
public function tags() {
return $this->belongsToMany(Tag::class);
}
// الوظائف المساعدة
public function getFinalPriceAttribute() {
return $this->price - ($this->price * $this->discount_percentage / 100);
}
public function scopeFeatured($query) {
return $query->where('is_featured', true);
}
}
نموذج الطلب (Order)
class Order extends Model
{
const STATUS_PENDING = 'pending';
const STATUS_PROCESSING = 'processing';
const STATUS_SHIPPED = 'shipped';
const STATUS_DELIVERED = 'delivered';
protected $fillable = [
'order_number', 'consumer_id', 'total', 'status',
'payment_status', 'shipping_address'
];
// العلاقات
public function user() {
return $this->belongsTo(User::class, 'consumer_id');
}
public function products() {
return $this->belongsToMany(Product::class, 'order_products')
->withPivot('quantity', 'single_price');
}
// النطاقات
public function scopeActive($query) {
return $query->whereNotIn('status', ['cancelled']);
}
}
المتحكمات (Controllers)
المتحكمات الرئيسية وطرق التعامل مع البيانات:
متحكم المنتجات
class ProductController extends Controller
{
public function index(Request $request)
{
$query = Product::with(['brand', 'category', 'images']);
// البحث
if ($request->filled('search')) {
$query->where('name', 'like', '%' . $request->search . '%');
}
// الفلترة حسب الفئة
if ($request->filled('category')) {
$query->where('category_id', $request->category);
}
// الترتيب
switch ($request->get('sort', 'latest')) {
case 'price_low':
$query->orderBy('price', 'asc');
break;
case 'price_high':
$query->orderBy('price', 'desc');
break;
default:
$query->orderBy('created_at', 'desc');
}
return $query->paginate(12);
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'stock' => 'required|integer|min:0',
'brand_id' => 'required|exists:brands,id',
'category_id' => 'required|exists:categories,id'
]);
$product = Product::create($validated);
return response()->json($product, 201);
}
}
متحكم الطلبات
class OrderController extends Controller
{
public function store(Request $request)
{
$validated = $request->validate([
'products' => 'required|array',
'products.*.id' => 'required|exists:products,id',
'products.*.quantity' => 'required|integer|min:1',
'shipping_address' => 'required|string'
]);
DB::transaction(function () use ($validated) {
$order = Order::create([
'order_number' => $this->generateOrderNumber(),
'consumer_id' => auth()->id(),
'status' => Order::STATUS_PENDING,
'shipping_address' => $validated['shipping_address']
]);
$total = 0;
foreach ($validated['products'] as $productData) {
$product = Product::find($productData['id']);
$order->products()->attach($product->id, [
'quantity' => $productData['quantity'],
'single_price' => $product->price
]);
$total += $product->price * $productData['quantity'];
}
$order->update(['total' => $total]);
});
return response()->json(['message' => 'تم إنشاء الطلب بنجاح']);
}
}
دليل التثبيت والإعداد
خطوات تثبيت وإعداد منصة Peak Link:
المتطلبات
- PHP 8.1 أو أحدث
- Composer
- MySQL 8.0 أو أحدث
- Node.js و npm
- Redis (اختياري للتخزين المؤقت)
خطوات التثبيت
# 1. استنساخ المشروع
git clone https://github.com/peak-link/ecommerce.git
cd ecommerce
# 2. تثبيت التبعيات
composer install
npm install
# 3. نسخ ملف البيئة
cp .env.example .env
# 4. إنشاء مفتاح التطبيق
php artisan key:generate
# 5. إعداد قاعدة البيانات في .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=peak_link
DB_USERNAME=root
DB_PASSWORD=
# 6. تشغيل الهجرات
php artisan migrate
# 7. تشغيل البذور
php artisan db:seed
# 8. إنشاء رابط التخزين
php artisan storage:link
# 9. بناء الأصول
npm run build
# 10. تشغيل الخادم
php artisan serve
إعدادات إضافية
# إعداد الطوابير
QUEUE_CONNECTION=database
php artisan queue:work
# إعداد المهام المجدولة
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
# إعداد Redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
# إعداد البريد الإلكتروني
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
الأوامر المفيدة
# تحديث الكاش
php artisan config:cache
php artisan route:cache
php artisan view:cache
# إنشاء مستخدم مدير
php artisan make:admin
# تحديث فهرس البحث
php artisan scout:import "App\Models\Product"
# تنظيف الملفات المؤقتة
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear