Undefined eager-loaded relationship when using Echo event broadcasting - laravel

I am using Laravel Echo in my project to broadcast events to the client-side. Everything is working correctly, however, I am having a problem in eager-loading the relationships of the model for the Event.
My Controller
$chat = new Chat;
$chat->user_id = $request->user_id;
$chat->message = $request->message;
$chat->save();
event( new GetMessages( $chat ));
My Event class:
class GetMessages implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $chats;
public function __construct($chats)
{
$this->chats = $chats;
}
public function broadcastOn()
{
return ['get-messages'];
}
public function broadcastAs()
{
return 'get-messages';
}
}
Here is my Laravel Echo
Echo.channel('get-messages')
.listen('.get-messages', (data) => {
console.log(data)
})
Everything is working fine as I expected, except getting the attachments from the Chat.
console.log(data.chats); // Undefined
Is there something I missed? Because as I tested the eager-loaded from the Event class itself, I can access the attachments in there.
public $chats;
public function __construct($chats)
{
$this->chats = $chats;
echo $chats->attachments // It's working in here, I can get the attachments
}
Hope I explain it well, Thank you in advance
Cheers!
Here is the console.log
Here is use, it's the default when I created the Event
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 GetMessages implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
...

Related

How to run MQTT subscribe in the background in Laravel

I am trying to put MQTT subscribe into a job saved in the database, but I get an error message that the job has failed instead of running for an infinite time. I would like to know if my approach is even possible and if there are any better alternatives to my problem.
My code
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use PhpMqtt\Client\Facades\MQTT;
class StartSub implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct()
{
//
}
public function handle()
{
$mqtt = MQTT::connection();
$mqtt->subscribe('topic', function (string $topic, string $message) {
echo sprintf('Received QoS level 1 message on topic [%s]: %s',
$topic, $message);
}, 1);
$mqtt->loop(true);
}
}
The answer is pretty simple. Although I do not know if it is 100% correct. After further investigation I found out that my code was failing because of time out. So i just had to put a public $timeout = 0;.

How to change the subject of email in mailable class in laravel

How to change the subject of email in mailable class in laravel 6. The subject on email showing is 'Send Email'. email is sending properly. the only problem is I am not able to change the subject of the email.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendEmail extends Mailable
{
use Queueable, SerializesModels;
protected $user;
public function __construct($user)
{
$this->user = $user;
}
public function build()
{
return $this->view('mails.verify_account')->with([
'email_token' => $this->user->email_token,
]);
}
}
You may use subject method:
public function build()
{
return $this->subject('Your subject')
->view('mails.verify_account')
->with([
'email_token' => $this->user->email_token,
]);
}
Check Laravel docs for more info.
in your __construct, add: $this->subject('bla bla');

How to load object into Laravel 5.5 Mailable?

I'm trying to test a mailable class for a Laravel 5.5 application, and have seemingly followed all the directions here.
I've been trying to preview my mailable in the browser, but for some reason the object that I pass to my mailable class is not passing correctly, and is showing as null from inside the mailable class even though I can see that it exists outside of it.
Here is the function in my web.php file that I'm using to test the mailable in the browser:
Route::get('/testingMailable', function () {
$review = App\EvaluationReview::find(6);
return new App\Mail\StartReview($review);
});
The object is populating as it should above, but when I try to view it inside the mailable class, it is null:
<?php
namespace App\Mail;
use App\EvaluationReview;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class StartReview extends Mailable
{
use Queueable, SerializesModels;
public $review;
public function __construct(EvaluationReview $review)
{
$this->$review = $review;
dd($this->review);//is null
}
public function build()
{
return $this->from('test#test.com')->view('startReview');
}
}
Does anyone know why the object is not getting passed into the mailable class?
You have a typo in your code.
Change:
$this->$review = $review;
To:
$this->review = $review;

how to broadcast event called by job class

In a Laravel project i am trying to broadcast a event named "closed" via pusher.
In my controller i am calling:
App\Jobs\Closing::dispatch();
My App\Jobs\Closing.php:
<?php
namespace App\Jobs;
use App\Jobs\Close;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class Closing implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(){}
public function handle()
{
$delay = mt_random(10,20);
Close::dispatch()->delay(now()->addSeconds($delay));
}
}
My app\Jobs\Close.php:
<?php
namespace App\Jobs;
use App\Events\Closed;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class Close implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(){}
public function handle()
{
event(new Closed());
}
}
My App\Events\Closed.php:
<?php
namespace App\Events;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class Closed implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(){}
public function broadcastOn()
{
return ['updates'];
}
public function broadcastAs()
{
return 'closed';
}
public function broadcastWith()
{
return ['message' => 'Is Closed!'];
}
}
At this point i need to explain two mandatory situations.
The first one is that i needed to create the job Close class as a job because i needed to delay the execution of the task close. And the only way i can see to make this "delay" is with a Job Class.
The second one is that i have chosen to implement "ShouldBroadcastNow" instead of "ShouldBroadcast" in the event Closed class because i don't want to queue the broadcasting.
Now the problem is that after running:
php artisan queue:work --tries=1
i get in the following output on Command Console:
Processing: App\Jobs\Closing
Processed: App\Jobs\Closing
Processing: App\Jobs\Close
Processing: App\Events\Closed
Failed: App\Events\Closed
Failed: App\Jobs\Close
The first thing that i find weird is that App\Events\Closed goes to queue despite the fact that it implements "ShouldBroadcastNow".
On laravel.log it seems that it occurred a BroadcastException at PusherBroadcaster.php.
But if in the Controller i do:
event(new App\Events\Closed());
the event is properly broadcast via pusher to the client browser.
What is going wrong?
Is there other way do delay the "close" without jobs?
The purpose is to have the following workflow:
1 - We have a event named "closing" an another event named "closed";
2 - We have a task named "close" that occurs "x" seconds after the event "closing", where "x" is a random number;
3 - After the execution of "close" task we broadcast the event "closed".
Thank you for your attention to my problem
Meanwhile i found this is a already known issue (https://github.com/laravel/framework/issues/16478).
It can be solved in two ways:
1 - editing the relevants php.ini files (in my case it was mamp/conf/php7.0.9/php.ini and mamp/bin/php/php7.0.9/php.ini) to point at the location of curl certificate;
2 - editing config/broadcasting.php (setting encrypted to false in pusher options).

Why broadcasting channel public not working? Laravel

I use laravel 5.3
I make routes/channels.php like this :
<?php
Broadcast::channel('messages', function() {
return true;
});
If I input the data cart and click submit, it will run this :
this.$http.post(window.BaseUrl + '/guest/add-notification', {cart_data: JSON.stringify(data)});
It will call function on the controller
The function like this :
public function addNotification(Request $request){
$input = $request->only('cart_data');
$data = json_decode($input['cart_data'], true);
event(new CartNotificationEvent($data));
}
Then it will call event
The event like this :
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class CartNotificationEvent
{
use InteractsWithSockets, SerializesModels;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function broadcastWith()
{
return [
'message' => $this->data,
];
}
public function broadcastAs()
{
return 'newMessage';
}
public function broadcastOn()
{
return new Channel('messages');
}
}
On the client, I do like this :
Echo.channel('messages')
.listen('.newMessage', (message) => {
console.log('test')
console.log(message);
});
When all the code is executed, I check on the console, the console.log not display
Why is it not working?
If I see the whole code that I make, it seems the process is correct
class CartNotificationEvent implements ShouldBroadcast is missing.

Resources