DEV Community

Jitesh Dhamaniya
Jitesh Dhamaniya

Posted on

Dispatch a job from tinker in Laravel

When you need to dispatch job from tinker, the normal command which is

App\Jobs\YourJob::dispatch();

would not work in tinker because dispatch helper function depends on garbage collection. Hence to dispatch a job from tinker, use any of the following commands

\Bus::dispatch(new App\Jobs\YourJob($someArgument));
Enter fullscreen mode Exit fullscreen mode
\Queue::push(new App\Jobs\YourJob($someArgument)); 
Enter fullscreen mode Exit fullscreen mode

Reference - https://github.com/laravel/tinker/commit/1baadfe1721f85cd4e776aa323128dcd329f170d#diff-04c6e90faac2675aa89e2176d2eec7d8

Top comments (6)

Collapse
 
bambamboole profile image
Manuel Christlieb

nice thx!

Collapse
 
hosseinm1997 profile image
Hossein Moshfegh

Thanks.

Collapse
 
awaegelrm profile image
awaegel-rm

Any way to add one of these to a specific queue? Using onQueue like we do in standard Laravel code throws an error:
\Bus::dispatch(new \App\Jobs\My Job())->onQueue('long-running-queue')
PHP Error: Call to a member function onQueue() on string in /home/my/siteeval()'d code on line 1

Collapse
 
edvordo profile image
edvordo

Late answer, but the Bus::dispatch method determines the queue from the command/job itself

Collapse
 
ilejohn profile image
Opeyemi Ilesanmi

You just saved me from some more frustrated hours. Thanks!

Collapse
 
mizouzie profile image
Sam

Glad that this is still here! I forgot what is was since the last time. Thanks a lot!