Passing variables to Evens in Laravel 5.5 - php

How i can pass some variables to an Event in Laravel 5.5?
I have tried in several mode but not working. Here is my code. Someone have any suggestion?
Basically i need to update the number of followers via socket. Server using Redis and Socket.io and work as well
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);
});
And this is the Event
<?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'];
}
}

You can pass it as Constructor's parameter in your Follow Event class, and if you need it as public field do like this:
<?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'];
}
}
And pass him parameters in this way:
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));
});

Related

How to set a broadcast driver in the event of laravel?

I have an event like this
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NotifyUser implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
protected $user_id;
public $message;
public $link;
public $notification_id;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($notification)
{
$this->notification_id = $notification->id;
$this->user_id = $notification->user_id;
$this->message = $notification->message;
$this->link = $notification->link;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('notification.user.'.trim($this->user_id));
}
}
And I have configured both pusher and mercury, is it possible to choose which broadcasting driver to use per event? Instead of always using the default?

Argument #1 ($mailable) must be of type Illuminate\Contracts\Mail\Mailable

I try to send an email to a new user. But I get this error, but I don't understand what is wrong.
TypeError: Illuminate\Mail\PendingMail::send(): Argument #1
($mailable) must be of type Illuminate\Contracts\Mail\Mailable,
App\Events\UserStored given, called in
/Users/kate_kimt/laravel/laravel_block/app/Listeners/SendMailNewUser.php
on line 31 in file
/Users/kate_kimt/laravel/laravel_block/vendor/laravel/framework/src/Illuminate/Mail/PendingMail.php
on line 122
UserStored.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\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
class UserStored
{
public User $user;
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
SendMailNewUser.php:
namespace App\Listeners;
use App\Events\UserStored;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Mail;
use Illuminate\Mail\Mailable;
class SendMailNewUser extends Mailable
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param \App\Events\UserStored $event
* #return void
*/
public function handle(UserStored $event)
{
Mail::to($event->user->email)->send(new UserStored($event->user));
}
}

How to resolve 'The expected [App\Events\UserRegistered] event was not dispatched.' error?

I have created a feature called test to test the admin registration. When i run the testcases it gives me this error
1) Tests\Feature\Admin\RegisterTest::testValidRegistration
The expected [App\Events\UserRegistered] event was not dispatched.
Failed asserting that false is true.
/var/www/html/gas-backend/tests/Feature/Admin/RegisterTest.php:85
Below i have stated the admin registration test file and user registered event file
Admin/RegisterTest.php
$response->assertStatus(201);
$response->assertJson($expectedResponse);
$this->assertDatabaseHas('users', $expectedDatabaseRecord);
Event::assertDispatched(UserRegistered::class);
Notification::assertNotSentTo($user, RegistrationComplete::class);
UserRegistered.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\Models\User;
class UserRegistered
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
//return new PrivateChannel('channel-name');
}
}
How should i resolve this error ?

Laravel detach in event not working

I am deleting a filter which model looks like this:
namespace App;
use App\Events\FilterDelete;
use Illuminate\Database\Eloquent\Model;
class Filter extends Model
{
protected $events = [
'deleting' => FilterDelete::class
];
public function filterGroup()
{
return $this->belongsTo('App\FilterGroup');
}
public function products()
{
return $this->belongsToMany('App\Product', 'product_filters')->withPivot('id', 'quantity', 'price');
}
}
The FilterDelete looks like this:
namespace App\Events;
use App\Filter;
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;
class FilterDelete
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $filter;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Filter $filter)
{
$this->filter = $filter;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
And the listener looks like this:
namespace App\Listeners;
use App\Events\FilterDelete;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class DeleteProductFilter
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param FilterDelete $event
* #return void
*/
public function handle(FilterDelete $event)
{
$event->filter->products()->detach();
}
}
And when I delete a filter the products are not detached and the records stay in the database. No errors are returned, evetywhing seems to be working fine. Can anyone tell me why the products are not detaching ?

job class is not accepting the argument send from controller in laravel

Error: Missing argument 1 for App\Jobs\ReorderDatabase::handle() is showing,
I need to pass the variable from controller and i need not to use the model,
so How I should proceed.
My controller function code is here
public function postData(Request $request)
{
$updateRecordsArray = Input::get('order');
$this->dispatch(new ReorderDatabase($updateRecordsArray));
return Response::json('Okay');
}
My job RecorderDatabase job code is
<?php namespace App\Jobs;
use App\Http\Requests\Request;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\DragDropController;
/**
* Class ReorderDatabase
* #package App\Jobs
*/
class ReorderDatabase extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* #return void
*/
public function handle($updateRecordsArray)
{
$i = 1;
foreach ($updateRecordsArray as $recordID) {
DB::table('venues')->where('id', '=', $recordID)->update(array('priority' => $i));
$i++;
}
}
}
As #lagbox mentioned, you need to pass this argument into constructor and not handle method.
Your job class should look like this:
<?php namespace App\Jobs;
use App\Http\Requests\Request;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\DragDropController;
/**
* Class ReorderDatabase
* #package App\Jobs
*/
class ReorderDatabase extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $updateRecordsArray;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($updateRecordsArray)
{
$this->updateRecordsArray = $updateRecordsArray;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$i = 1;
foreach ($this->updateRecordsArray as $recordID) {
DB::table('venues')->where('id', '=', $recordID)->update(array('priority' => $i));
$i++;
}
}
}

Categories