2

I am new to laravel, I have a little difficulty. Is giving this error does anyone know how to fix?

 <?php

namespace App\Events;

use App\Models\Order;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class OrderChangedEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    /**
     * @var Order
     */
    public $order;

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

"Argument 1 passed to App\Events\OrderChangedEvent::__construct() must be an instance of App\Models\Order, string given, called in /home/delivery/public_html/lar ▶"

  • how are you dispatching this event? can you please share that code. – golangphp Jun 21 '20 at 05:33
  • I believe you are passing wrong parameter from the controller. The constructor is expecting for an Order, but you are passing a string. There is another problem in your constructor, you need to include the Model's own constructor method. Please refer to this answer https://stackoverflow.com/a/30503372/7882426 – Tharavink Prasad Sivarajan Jun 21 '20 at 05:34

1 Answers1

2

What does it look like from where you are triggering this event?

It should look like:

...

event(new OrderChangedEvent($order));
...

And $order needs to be an instance of the model Order then passed. For example:

$order = Order::findOrFail($id);

...

event(new OrderChangedEvent($order));

jeremykenedy
  • 4,150
  • 1
  • 17
  • 25