Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jnsyhvw56's avatar

Batch Jobs and Chains Error

Hi,

According to the docs here: https://laravel.com/docs/8.x/queues#chains-within-batches

It should be possible to place multiple chains (as arrays) within the batch array.

My understanding of this is that the following should work:

	// Some controller

        $batch = Bus::batch([
                [
                    new PrepareOutput($output_queue[0], $template),
                    new CreateOutput($output_queue[0]),
                ],
                [
                    new PrepareOutput($output_queue[1], $template),
                    new CreateOutput($output_queue[1]),
                ],                
            ])
            ->then(function (Batch $batch) {
                // All jobs completed successfully...
            })
            ->catch(function (Batch $batch, Throwable $e) {
                // First batch job failure detected...
            })
            ->finally(function (Batch $batch) {
                // The batch has finished executing...
            })
            ->dispatch();

	return $batch->id;

However I'm receiving the error:

Call to a member function withBatchId() on array

When dispatch() is called.

There's not much discussion/support/articles around the new batch feature yet and I'm struggling to uncover exactly what's going wrong.

All help appreciated

0 likes
8 replies
xtfer's avatar

Still getting this on 8.26 :/

mahmoudakoubeh's avatar

You need to import this trait in your job class (Batchable)

use Illuminate\Bus\Batchable;

17 likes
azimidev's avatar

the Bus::batch() method expects a flat array of jobs, not arrays of job chains. When using batches, you cannot directly nest arrays to represent job chains. Each job in the batch must be a single job instance, not an array.

$batch = Bus::batch([
    Bus::chain([
        new PrepareOutput($output_queue[0], $template),
        new CreateOutput($output_queue[0]),
    ]),
    Bus::chain([
        new PrepareOutput($output_queue[1], $template),
        new CreateOutput($output_queue[1]),
    ]),
])
    ->then(function (Batch $batch) {
        // All jobs completed successfully...
    })
    ->catch(function (Batch $batch, Throwable $e) {
        // First batch job failure detected...
    })
    ->finally(function (Batch $batch) {
        // The batch has finished executing...
    })
    ->dispatch();

return $batch->id;

Please or to participate in this conversation.