mercredi 22 août 2018

Laravel Model Serialization with DB transaction

I have an event that should be triggered, however, when I call create method which uses DB::transaction, I get an error message saying No query results for model [App\Model\Order\Order]. I understand I get the error message because the event is trying to serialize OrderModel which is not created yet because of DB::transaction.

What can be done to avoid this error?

Any advice or suggestion would be appreciated. Thank you in advance.

Controller

public function create() {
    DB::transaction(function () {
         $order = OrderModel::create([
                 'content' => 'bla bla'
             ]);

         event(new OrderDispatched($order));

         .... some other logics...
    });


    return response($order, 200);
}

Event

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

    public $order;

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

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

    public function broadcastWith()
    {
        return [
            'id' => $this->order->id,
            'carrier_team_id' => $this->order->carrier_team_id
        ];
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire