Mar 7, 2025
0
Level 1
Provide displayName() for Queued listener.
I have a queueable listener, I want to provide custom displayName() function to it.
<?php
namespace App\Listeners;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class HelloWorld implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
private static array $names = [
'John',
'Viggo',
'Winston'
];
private string $personName;
public function shouldQueue(\App\Events\HelloWorld $event): bool
{
return in_array($event->personName, self::$names);
}
/**
* Handle the event.
*/
public function handle(\App\Events\HelloWorld $event): void
{
\App\Jobs\HelloWorld::dispatch($event->personName);
}
public function displayName(): string
{
// Also how can I have the $personName here?
return 'HelloWorld Listener';
}
}
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class HelloWorld implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct(private readonly string $personName)
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
//
}
public function displayName(): string
{
return 'Executing HelloWorld job for ' . $this->personName;
}
}

Please or to participate in this conversation.