I have worked code:
Frontend code
resources/js/components/OnlineWall.vue
:
mounted() {
window.Echo.channel('laravel_database_new-payload')
.listen('.new-payload-event', (e) => {
console.info('listen');
console.log(e.payload);
})
}
Event code app/Events/NewPayload.php
:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use App\Payload;
class NewPayload implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $payload;
/**
* NewPayload constructor.
* @param Payload $payload
* @return void
*/
public function __construct(Payload $payload)
{
$this->payload = $payload;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('new-payload');
}
/**
* Custom broadcast message name
*
* @return string
*/
public function broadcastAs()
{
return 'new-payload-event';
}
}
If I fire event: NewPayload::dispatch($testNewPayload);
all works fine.
But I need to pass integer variable from vue component to laravel event (city_id)
if city_id is identical to city_id of Payload model from event (in $testNewPayload), then fire event . Otherwise don't fire event.
It is possible?
Channel is public, Laravel 7.0
Thank you!
Solved.
mounted() {
window.Echo.channel(`laravel_database_new-payload.${this.city_id}`)
.listen('.new-payload-event', (e) => {
console.info('listen');
console.log(e.payload);
})
}
public function broadcastOn()
{
return new Channel('new-payload.'.$this->payload->city_id);
}