Home » Php » php – Why Laravel Tinker must be restarted for code to take effect?

php – Why Laravel Tinker must be restarted for code to take effect?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m testing Laravel broadcasting events using Tinker. Each time i made a change in my code source i must restart tinker to take effect.

enter image description here

App\Events\ShippingStatusUpdated.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ShippingStatusUpdated implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;


    public $update;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($update)
    {
        $this->update = $update;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('order-5');
    }

    public function broadcastWith() {
        return ['status' => $this->update];
    }
}

resources\bootstrap.js

//...
import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true
});

let orderId = 5;

window.Echo.channel(`order-${orderId}`)
.listen('ShippingStatusUpdated', (e) => {
    console.log(e);
});

Preview

enter image description here

*1st attempt : code change (added construct to ShippingStatusUpdated calass ) + same Tinker instance : getting (status: null)

*2nd attempt after restarting Tinker : getting (status: 3)

Why is that happened and how to force Tinker consider code source changes ?

How to&Answers: