يجب إنشاء علاقات بين الجداول من نوع one to many ثم استخدام تقنيات الزمن الحقيقي لتحقيق المطلوب.
قم بتقسيم المهمة إلى 6مراحل فرعية:
إنشاء جدول مستخدمين users يحوي الحقول التالية id - name - email
إنشاء جدول للرسائل messages يحوي الحقول التالية: id - from - content - to
بناء one to many relationship داخل ال models: User, Message
إنشاء ال form الخاص بكتابة الرسالة وتحديد المرسل إليه
إنشاء div لعرض محتوى الرسائل الواردة
استخدام الزمن الحقيقي real-time لاستقبال الرسالة في نفس اللحظة التي تم الإرسال بها دون الحاجة للقيام بتحديث الصفحة
لماذا نحتاج علاقة من نوع one to many ؟
لأن المستخدم يستطيع إرسال الرسائل لأكثر من مستخدم، تستطيع الاطلاع على التوثيق الرسمي للارافل من هنا:
User
php artisan make:model User -m
// User Model
public function from(){
return $this->hasMany(Message::class, 'from');
}
public function to(){
return $this->hasMany(Message::class, 'to');
}
// users migration
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password')->nullable();
$table->rememberToken();
$table->timestamps();
});
Message
php artisan make:model Message -m
// Message Model
public function user(){
return $this->belongsTo(User::class, 'from');
}
// messages migration
Schema::create('messages ', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('content');
$table->unsignedBigInteger('from');
$table->foreign('from')->references('id')->on('users');
$table->unsignedBigInteger('to');
$table->foreign('to')->references('id')->on('users');
$table->timestamps();
});
التعليقات