PhiloNL / philo-dev-comments

0 stars 0 forks source link

laravel-batches-and-real-time-progress-with-livewire/ #2

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Laravel Batches and real-time progress with Livewire

Learn how to get started with Laravel Batches and how to show the progression in real-time with Livewire

https://philo.dev/laravel-batches-and-real-time-progress-with-livewire/

mikield commented 3 years ago

This is a awesome example using a real-time upload with batch and Laravel Echo. Thank you for your article. 🎉

ibes commented 3 years ago

Just wrapping my head around batch and appreciate a lot that you are sharing your process here. Will most likely take some inspirations from your code. Thanks you!

ibes commented 3 years ago

Can someone help me out. I would like to log information in the callback functions of the Batch.

eg in the finally here

$batch = Bus::batch($jobs)
            ->finally(function () use ($transfer) {
                TransferCompleted::dispatch($transfer);
                // logger('finally I am HERE')
            })->dispatch();

But for me, that does not work. It can be because I totally fail to understand what is possible in there. Or I have a strage setup without knowig that.

Logging doesn't work at all in neither finally nor then or catch. And other things behave differently. Eg. $transfer->update() doesn't work - but $transfer->value = 'some value'; $transfer->save() does work.

I don't get it and I would like to understand what I am failing to understand. Happy for any hint :)

mikield commented 3 years ago

@ibes first what I got in my mind: try to pass $transfer variable by reference using &$transfer.

Maybe I do not understand your problem, but if you do $transfer->update() and it doest not updates the values set before finally - it could be cause of Model Serialization, and Laravel doest not serialise any "dirty" values of Model. So passing it by reference will already serialise a modified version of it.

Also please remember: finally will be called even if Batch will fail. So maybe you do not want to update your model in finally block.

davisngl commented 2 years ago

This is super valuable. Thanks for sharing this, Philo :smiley:

gavinsbtm commented 1 year ago

Very new to the Laravel framework and tried to recreate this a few times (Laravel 9) but I get "Call to undefined method App\Models\User::transfers()" when the initiateTransfer() function in the Livewire component is called. Am I doing something obviously wrong?

gavinsbtm commented 1 year ago

Ok... I think I found a solution.

I'm trying this code with the fortress & jetstream user accounts & teams. I had to add the following code to the User model...

use Illuminate\Database\Eloquent\Relations\HasMany;

public function transfers(): HasMany
    {
    return $this->hasMany(Transfer::class);
    }
ricardoaponte commented 1 year ago

Hello Philo, I was having a hard time figuring out this error: Cannot assign string to property App\Http\Livewire\EditQuestion::$newFiles of type array.

Your Tip on typed public properties gave me the the solution. I had this: public array $newFiles = [];, changed it to public $newFiles = []; and the error went away.

Not sure why this happens.