Laravel BroadcastAs 中的 Pusher 傳遞參數
P粉340264283
P粉340264283 2024-03-31 20:27:44
0
1
377

嗨,我有這個 js 程式碼

var pusher = new Pusher('my pusher key', {
        cluster: 'ap2'
    });
    var channel = pusher.subscribe('my-channel');

    channel.bind('my-event', function(data)
    {
        console.log(data);
    });

這是我的 Laravel 程式碼

protected $pos_invoice;
public function __construct($pos_invoice)
{
    $this->pos_invoice = $pos_invoice;
}
public function broadcastOn()
{
    return new Channel('my-channel');
}

public function broadcastAs()
{
    return 'my-event';
}

這是呼叫程式碼

return event( new \App\Events\New_pos_online_order_event('aa'));

現在是程式碼

channel.bind('my-event', function(data)
    {
        console.log(data);
    });

總是在控制台上返回[],所以我嘗試了這個

public function broadcastAs()
{
    return 'my-event.'.$this->pos_invoice;
}

還有這個

public function broadcastOn()
{
    return new Channel('my-channel'.'asdfasdf');
}

當我更改任何內容時

public function broadcastOn()
{
    return 'my-channel';
}

public function broadcastAs()
{
    return 'my-event';
}

程式碼不起作用並且在控制台上沒有返回任何內容 那我該如何使用 js 在 Pusher 和 Laravel 上傳遞參數 謝謝..

P粉340264283
P粉340264283

全部回覆(1)
P粉662802882

您需要定義函數broadcastWith

**
 * Get the data to broadcast.
 *
 * @return array
 */
public function broadcastWith()
{
    return ['pos_invoice' => $this->pos_invoice];
}

您將在綁定函數的資料中收到陣列

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!