Framework: [5.5] dispatch() helper returns object

Created on 17 Oct 2017  路  3Comments  路  Source: laravel/framework

  • Laravel Version: 5.5.13
  • PHP Version: 7.1.10
  • Database Driver & Version: 15.1 Distrib 10.2.9-MariaDB

Description:

As I pointed out in a stackoverflow question, the dispatch() helper now returns a PendingDispatch object, instead of the id of the job.

5.4
vs.
5.5

In fact, trying to access the result of the dispatch() helper returns:
Object of class Illuminate\Foundation\Bus\PendingDispatch could not be converted to string

What if I want to retrieve the job id of the newly created job in the queue, as I did before?
I didn't find anything in the documentation or in the upgrade guide.

Steps To Reproduce:

$job = (new JobClass());
$jobId = dispatch($job);

echo $jobId; // Generates exception

All 3 comments

You can use this to have the same output as in 5.4:

app(\Illuminate\Contracts\Bus\Dispatcher::class)->dispatch($job)

Thank you, this solves the problem. Might be the case to add this to the 5.4 -> 5.5 upgrade guide docs?

The DispatchesJob trait makes this easier. It's applied to controllers by default but it can be added to other classes too.

Was this page helpful?
0 / 5 - 0 ratings