Reputation: 1
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
<?php
namespace App\Observers;
use App\Models\User;
use App\Models\ServiceOrder;
use Filament\Notifications\Notification;
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
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;
class ServiceOrder extends Model { use HasFactory;
// 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
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();
});
}
Upvotes: 0
Views: 110
Reputation: 3045
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:
// 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:
$assignedStaff = User::whereHas('serviceOrders', function($query) use ($serviceOrder) {
$query->where('id', $serviceOrder->id);
})
->get();
Or ideally:
$assignedStaff = User::whereHas(
'serviceOrders',
fn ($query) => $query->whereKey($serviceOrder->id)
)
->get();
Upvotes: 0