2017-10-19 4 views
0

Comment puis-je transmettre certaines variables à un événement dans Laravel 5.5? J'ai essayé plusieurs modes mais je ne travaillais pas. Voici mon code. Quelqu'un a une suggestion? Fondamentalement, j'ai besoin de mettre à jour le nombre de disciples via socket. Server à l'aide Redis et Socket.io et travailler ainsiPasser des variables à Evens dans Laravel 5.5

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); 


}); 

C'est l'événement

<?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']; 
    } 
} 

Répondre

0

Vous pouvez passer comme paramètre de constructeur dans votre Suivez classe l'événement, et si vous en avez besoin que du public domaine comme cela:

<?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']; 
    } 
} 

Et lui passer des paramètres de cette façon:

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)); 


});