Search code examples
laravelwebsocketreal-timelaravel-8broadcasting

Problem with broadcasting and websocket in Laravel


I don't understand where it is blocking. If I replace the update() with a create() method, it works, or if I remove the broadcast, the update works. How to update with the broadcast?

Call to a member function load() on bool

public function SendToMarket(Request $request, $id)
{
    $card = auth()->user()->cards()->findOrFail($id)->update([
            'market_id' => $request["_market"],
            'borderStyle_id' => $request["_borderStyle"],
            'price' => $request["_price"]
        ]
    );

    $notification = array(
        'message' => 'Card Send to market Successfully !',
        'alert-type' => 'warning'
    );

    broadcast(new FetchCardEvent($card->load('user')))->toOthers();

    return redirect()->route('user.cards')->with($notification);
}

Solution

  • update() does not return the Modal instance. Instead it will return a Boolean.

    After retrieving the card using firstOrFail() you should update it separately.

    public function SendToMarket(Request $request, $id) {
        $card = auth()->user()->cards()->findOrFail($id);
    
        $card->update([
            'market_id' => $request["_market"],
            'borderStyle_id' => $request["_borderStyle"],
            'price' => $request["_price"]
        ]);
    
        $notification = array(
            'message' => 'Card Send to market Successfully !',
            'alert-type' => 'warning'
        );
    
        broadcast(new FetchCardEvent($card->load('user')))->toOthers();
    
        return redirect()->route('user.cards')->with($notification);
    }