اذهب إلى المحتوى
  • 0

كيف يمكن تعميم كود خاص ب الاشعارات في لارافل

Osama Kha

السؤال

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Models\Faculty;
use Illuminate\Support\Facades\Auth;

class TaskComplete extends Notification
{
    use Queueable;
    private $details;
    private $task;
    /**
     * Create a new notification instance.
     */
    public function __construct(Faculty $task)
    {
        $this->task = $task;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['database'];
    }

    /**
     * Get the mail representation of the notification.
     */

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toDatabase($notifiable)
    {
        return [

            'id' => $this->task->Faculty_ID,
            'title' => 'هل توافق على اضافة كلية من قبل:',
            'User' => Auth::user()->name,
            'state' => 0
        ];
    }
}

هنا فقط يعمل عند اضافة كلية كيف يمكن التعديل على ذلك بحيث يكون نظام الاشعارات عام للموقع حاولت فكرة تكرار هذا الجزء مع انشاء blade خاص بكل محتوى لكن اتوقع خيار غير صحيح هل يوجد الية لتعميم ام انه يجب تكرار ماسبق 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

يمكنك التعامل مع فكرة التعميم عن طريق تمرير أكثر من وسيط إلى صنف الإشعار. مثال عملي: 

لنقل أننا نريد تخصيص عنوان الرسالة أو أي من معاملاتها، لنقم بتمرير العنوان نفسه كوسيط للباني: 

class TaskComplete extends Notification
{
    use Queueable;
    private $details;
    private $task;
    private $title;

    /**
     * Create a new notification instance.
     */
    public function __construct(Faculty $task, $title)
    {
        $this->task = $task;
        $this->title = $title;
    }

الآن يمكنك استخدام هاته الخاصية من داخل الصنف:

/**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toDatabase($notifiable)
    {
        return [

            'id' => $this->task->Faculty_ID,
            'title' => $this->title,
            'User' => Auth::user()->name,
            'state' => 0
        ];
    }

الآن ما عليك إلا تخصيص قيمة لعنوان الإشعار بحسب المكان الذي تقوم فيه باستدعاء الشيفرة المعنية. 

نفس الشيء يمكنك عمله مع أي فكرة يمكنك انطلاقا منها التوسع في تخصيص هذا الصنف. مثل تخصيص المحتوى أو غيرهأ.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

إليك مثالًا على كيفية تحديث صفك Notification ليكون أكثر عمومية
 

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Auth;

class GeneralNotification extends Notification
{
    use Queueable;

    private $details;

    /**
     * Create a new notification instance.
     *
     * @param array $details
     */
    public function __construct(array $details)
    {
        $this->details = $details;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        return [
            'id' => $this->details['id'],
            'title' => $this->details['title'],
            'user' => Auth::user()->name,
            'state' => $this->details['state'],
            // ........
        ];
    }
}

وعند إرسال الإشعار، يمكنك فعل ذلك بهذه الطريقة:

 

use App\Notifications\GeneralNotification;



$details = [
    'id' => $faculty->Faculty_ID,
    'title' => 'هل توافق على اضافة كلية من قبل:',
    'state' => 0,
    // ......
];

$user->notify(new GeneralNotification($details));

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...