How do I make the notification to be sent only to certain user based on assigned service order

I am trying to code a database notification using Filament in Laravel but it seems it won’t send out notification to users who was assigned to service order. Is it possible to do this way

ServiceorderOberserver.php Observer

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
namespace AppObservers;
use AppModelsUser;
use AppModelsServiceOrder;
use FilamentNotificationsNotification;
class ServiceOrderOberserver
{
/**
* Handle the ServiceOrder "created" event.
*/
public function created(ServiceOrder $serviceOrder): void
{
// Fetch the users who are assigned to this specific service order
$assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) {
$query->where('service_order_id', $serviceOrder->id);
})->get();
// Loop through each assigned staff member and send the notification
foreach ($assignedStaff as $staff) {
Notification::make()
->title('New Service Order: ' . $serviceOrder->pOrder)
->body('You have been assigned to service order ' . $serviceOrder->pOrder)
->success() // Styling for the notification (optional)
->sendToDatabase($staff); // Send notification to each staff member
}
}
/**
* Handle the ServiceOrder "updated" event.
*/
public function updated(ServiceOrder $serviceOrder): void
{
//
}
/**
* Handle the ServiceOrder "deleted" event.
*/
public function deleted(ServiceOrder $serviceOrder): void
{
//
}
/**
* Handle the ServiceOrder "restored" event.
*/
public function restored(ServiceOrder $serviceOrder): void
{
//
}
/**
* Handle the ServiceOrder "force deleted" event.
*/
public function forceDeleted(ServiceOrder $serviceOrder): void
{
//
}
}
</code>
<code><?php namespace AppObservers; use AppModelsUser; use AppModelsServiceOrder; use FilamentNotificationsNotification; class ServiceOrderOberserver { /** * Handle the ServiceOrder "created" event. */ public function created(ServiceOrder $serviceOrder): void { // Fetch the users who are assigned to this specific service order $assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) { $query->where('service_order_id', $serviceOrder->id); })->get(); // Loop through each assigned staff member and send the notification foreach ($assignedStaff as $staff) { Notification::make() ->title('New Service Order: ' . $serviceOrder->pOrder) ->body('You have been assigned to service order ' . $serviceOrder->pOrder) ->success() // Styling for the notification (optional) ->sendToDatabase($staff); // Send notification to each staff member } } /** * Handle the ServiceOrder "updated" event. */ public function updated(ServiceOrder $serviceOrder): void { // } /** * Handle the ServiceOrder "deleted" event. */ public function deleted(ServiceOrder $serviceOrder): void { // } /** * Handle the ServiceOrder "restored" event. */ public function restored(ServiceOrder $serviceOrder): void { // } /** * Handle the ServiceOrder "force deleted" event. */ public function forceDeleted(ServiceOrder $serviceOrder): void { // } } </code>
<?php

namespace AppObservers;

use AppModelsUser;
use AppModelsServiceOrder;
use FilamentNotificationsNotification;

class ServiceOrderOberserver
{
    /**
     * Handle the ServiceOrder "created" event.
     */
    public function created(ServiceOrder $serviceOrder): void
    {
        // Fetch the users who are assigned to this specific service order
        $assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) {
            $query->where('service_order_id', $serviceOrder->id);
        })->get();

        // Loop through each assigned staff member and send the notification
        foreach ($assignedStaff as $staff) {
            Notification::make()
                ->title('New Service Order: ' . $serviceOrder->pOrder)
                ->body('You have been assigned to service order ' . $serviceOrder->pOrder)
                ->success() // Styling for the notification (optional)
                ->sendToDatabase($staff); // Send notification to each staff member
        }
    }

    /**
     * Handle the ServiceOrder "updated" event.
     */
    public function updated(ServiceOrder $serviceOrder): void
    {
        //
    }

    /**
     * Handle the ServiceOrder "deleted" event.
     */
    public function deleted(ServiceOrder $serviceOrder): void
    {
        //
    }

    /**
     * Handle the ServiceOrder "restored" event.
     */
    public function restored(ServiceOrder $serviceOrder): void
    {
        //
    }

    /**
     * Handle the ServiceOrder "force deleted" event.
     */
    public function forceDeleted(ServiceOrder $serviceOrder): void
    {
        //
    }
}

I have tried reading the documentation but I can’t find any solution to my problems. I wanna add more details about my code. The Service Orders can assigned many staff so I have create a pivot table for that. My Service Order model looks like this

ServiceOrder.php model

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
</code>
<code><?php </code>
<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class ServiceOrder extends Model
{
use HasFactory;

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Fillable fields for mass assignment
protected $fillable = [
'pOrder',
'clientAddress',
'clientPhone',
'description',
'registeredDate',
'assignedDate',
'handoverDate',
'estimatedTime',
'status',
'remarks',
];
/**
* Define many-to-many relationship between ServiceOrder and User.
* A service order can have multiple staff members assigned.
*/
public function assignedStaff()
{
return $this->belongsToMany(User::class, 'service_order_staff', 'service_order_id', 'user_id')
->withTimestamps(); // Keeps track of when the staff was assigned
}
/**
* This relationship returns the users associated with the service order via the pivot table.
*/
public function users()
{
return $this->belongsToMany(User::class, 'service_order_staff', 'service_order_id', 'user_id');
}
</code>
<code>// Fillable fields for mass assignment protected $fillable = [ 'pOrder', 'clientAddress', 'clientPhone', 'description', 'registeredDate', 'assignedDate', 'handoverDate', 'estimatedTime', 'status', 'remarks', ]; /** * Define many-to-many relationship between ServiceOrder and User. * A service order can have multiple staff members assigned. */ public function assignedStaff() { return $this->belongsToMany(User::class, 'service_order_staff', 'service_order_id', 'user_id') ->withTimestamps(); // Keeps track of when the staff was assigned } /** * This relationship returns the users associated with the service order via the pivot table. */ public function users() { return $this->belongsToMany(User::class, 'service_order_staff', 'service_order_id', 'user_id'); } </code>
// Fillable fields for mass assignment
protected $fillable = [
    'pOrder',
    'clientAddress',
    'clientPhone',
    'description',
    'registeredDate',
    'assignedDate',
    'handoverDate',
    'estimatedTime',
    'status',
    'remarks',
];

/**
 * Define many-to-many relationship between ServiceOrder and User.
 * A service order can have multiple staff members assigned.
 */
public function assignedStaff()
{
    return $this->belongsToMany(User::class, 'service_order_staff', 'service_order_id', 'user_id')
                ->withTimestamps(); // Keeps track of when the staff was assigned
}

/**
 * This relationship returns the users associated with the service order via the pivot table.
 */
public function users()
{
    return $this->belongsToMany(User::class, 'service_order_staff', 'service_order_id', 'user_id');
}

}

And my pivot table looks like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function up(): void
{
Schema::create('service_order_staff', function (Blueprint $table) {
$table->id();
$table->foreignId('service_order_id')->constrained()->onDelete('cascade');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->timestamps();
});
}
</code>
<code>public function up(): void { Schema::create('service_order_staff', function (Blueprint $table) { $table->id(); $table->foreignId('service_order_id')->constrained()->onDelete('cascade'); $table->foreignId('user_id')->constrained()->onDelete('cascade'); $table->timestamps(); }); } </code>
public function up(): void
{
    Schema::create('service_order_staff', function (Blueprint $table) {
        $table->id();
        $table->foreignId('service_order_id')->constrained()->onDelete('cascade');
        $table->foreignId('user_id')->constrained()->onDelete('cascade');
        $table->timestamps();
    });
}

1

I have just read the documentation for Filament database notifications. Your code seems correct.

Have you registered the observer in the AppServiceProvider class?

Regarding sending database notifications:

Laravel sends database notifications using the queue. Ensure your queue is running in order to receive the notifications.

Do you have your queue and it’s worker up and running?

Edited:

Reading your code again, I believe the problem is at this line:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Fetch the users who are assigned to this specific service order
$assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) {
$query->where('service_order_id', $serviceOrder->id);
})
->get();
</code>
<code>// Fetch the users who are assigned to this specific service order $assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) { $query->where('service_order_id', $serviceOrder->id); }) ->get(); </code>
// Fetch the users who are assigned to this specific service order
$assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) {
    $query->where('service_order_id', $serviceOrder->id);
})
->get();

I don’t think your service_orders table has that service_order_id field. You might want to rewrite to this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) {
$query->where('id', $serviceOrder->id);
})
->get();
</code>
<code>$assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) { $query->where('id', $serviceOrder->id); }) ->get(); </code>
$assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) {
    $query->where('id', $serviceOrder->id);
})
->get();

Or ideally:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$assignedStaff = User::whereHas(
'serviceOrders',
fn ($query) => $query->whereKey($serviceOrder->id)
)
->get();
</code>
<code>$assignedStaff = User::whereHas( 'serviceOrders', fn ($query) => $query->whereKey($serviceOrder->id) ) ->get(); </code>
$assignedStaff = User::whereHas(
    'serviceOrders',
    fn ($query) => $query->whereKey($serviceOrder->id)
)
->get();

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật