【问题标题】:Passing variables to Evens in Laravel 5.5在 Laravel 5.5 中将变量传递给 Evens
【发布时间】:2017-10-19 23:51:05
【问题描述】:

如何在 Laravel 5.5 中将一些变量传递给事件? 我尝试了几种模式但没有工作。这是我的代码。有人有什么建议吗? 基本上我需要通过套接字更新关注者的数量。使用 Redis 和 Socket.io 的服务器,也可以正常工作

 Route::post('follow', function() {
    $negozio = Input::get('id_azienda');
    $followers = new \App\Models\Followers;
    $followers->entry_by = \Session::get('uid');
    $followers->id_azienda = $negozio;
    $followers->save();
    $this->variabili['negozio'] = $negozio;
    $this->variabili['followers'] = $followers->count();
    event(new \App\Events\Follow(), $this->variabili);


});

这就是事件

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

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

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $variabili;

    public function __construct()
    {
        $this->data = array(
            'count'=> $variabili['followers'],
            'negozio'=> $variabili['negozio']
        );
    }

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

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    您可以将它作为构造函数的参数传递到您的关注事件类中,如果您需要它作为公共字段,请执行以下操作:

        <?php
    
    namespace App\Events;
    
    use Illuminate\Broadcasting\Channel;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Broadcasting\PrivateChannel;
    use Illuminate\Broadcasting\PresenceChannel;
    use Illuminate\Foundation\Events\Dispatchable;
    use Illuminate\Broadcasting\InteractsWithSockets;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
    
    class Follow implements ShouldBroadcast
    {
        use Dispatchable, InteractsWithSockets, SerializesModels;
    
        public $data;
    
        /**
         * Create a new event instance.
         *
         * @return void
         */
        public function __construct($variabili)
        {
            $this->data = array(
                'count'=> $variabili['followers'],
                'negozio'=> $variabili['negozio']
            );
        }
    
        /**
         * Get the channels the event should broadcast on.
         *
         * @return \Illuminate\Broadcasting\Channel|array
         */
        public function broadcastOn()
        {
            return ['test-channel'];
        }
    }
    

    并以这种方式传递给他参数:

    Route::post('follow', function() {
        $negozio = Input::get('id_azienda');
        $followers = new \App\Models\Followers;
        $followers->entry_by = \Session::get('uid');
        $followers->id_azienda = $negozio;
        $followers->save();
        $this->variabili['negozio'] = $negozio;
        $this->variabili['followers'] = $followers->count();
        event(new \App\Events\Follow($this->variabili));
    
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 2022-01-11
      • 2020-02-11
      • 2016-12-19
      • 2018-07-23
      相关资源
      最近更新 更多