【问题标题】:Laravel event doesn't send the right data to PusherLaravel 事件没有向 Pusher 发送正确的数据
【发布时间】:2018-07-20 02:38:13
【问题描述】:

我有一个logController,它有show() 方法。在我的 show() 方法中,我返回了从数据库中检索到的单个数据。

$report = Log::select('logs.id')
  ->where('logs.id', '=', '1')
  ->get();
// event(new NewLog($report));
return $report;

在这种情况下,返回值为1。我还期望发送到 Pusher 的数据与查询结果相同。但是发送到 Pusher 的数据是 id, user_id、date` 等。我只想发送我获取的单个数据,即 logs.id。

这是我的 NewLog 事件:

class NewLog implements ShouldBroadcast
{
    public $log;

    use Dispatchable, InteractsWithSockets, SerializesModels;

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

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

【问题讨论】:

    标签: laravel events eloquent broadcast pusher


    【解决方案1】:

    您可以直接在 Log 上使用 find() 静态函数。 return $report 将为您提供对象。如果您想访问该对象的属性,请使用 $report->property_name;

    尝试使用以下代码:

    $report = Log::find(1);
    $event = event(new NewLog($report->id));
    return $report->id;
    

    $report = Log::select('logs.id')
      ->where('logs.id', '=', '1')
      ->get();
    // event(new NewLog($report->id));
    return $report;
    

    【讨论】:

    • 如果我有这样的查询怎么办? link ?它发送日志。* 不包括 users.first_name 和 users.last_name
    • 已解决。我添加了->toArray()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2023-04-05
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多