How to not add Notification broadcast in Queue? laravel 5.8 - laravel

what I want to achieve is to broadcast notification immediately instead of need to run
php artisan queue:listen
If in Event class I just need to Implements ShouldBroadcastNow to make events broadcast immediately. But I tried same things in Notification but it is not working.
Notification
<?php
namespace App\Notifications;
use App\Tournament;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
class TournamentAdded extends Notification implements ShouldBroadcastNow
{
use Queueable;
protected $tournament;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(Tournament $tournament)
{
$this->tournament = $tournament;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database', 'broadcast'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
$this->tournament
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
$this->tournament
]);
}
}
Execute notification
$t = Tournament::latest()->first();
$user->notify(new AppTournamentAdded($t));

Related

How can i use the Queue::after on Laravel?

I'm using Queue to send my mails on my application, and it's working great:
class SendMail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $user_mail;
private $person_data;
private $title;
private $company_name;
private $html;
private $email_sender;
private $email_reply;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($user_mail, $person_data, $title, $company_name, $html, $email_sender, $email_reply)
{
$this->user_mail = $user_mail;
$this->person_data = $person_data;
$this->title = $title;
$this->company_name = $company_name;
$this->html = $html;
$this->email_sender = $email_sender;
$this->email_reply = $email_reply;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Mail::to($this->user_mail)
->queue(new DocumentMessage($this->person_data, $this->title, $this->company_name, $this->html,
$this->email_sender, $this->email_reply));
}
}
Now I want to get the log of the moment when the emails were sent by the queue and, following the documentation, I put this code in my AppServiceProvider for testing:
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Carbon::setLocale('pt_BR');
setlocale(LC_ALL, 'pt_BR');
Carbon::setUtf8(true);
Paginator::useBootstrapThree();
Blade::withoutDoubleEncoding();
Queue::after(function (JobProcessing $event) {
DB::raw("INSERT INTO activity_log (log_name, description, subject_id, subject_type, causer_id, causer_type, properties)
VALUES ('email_sent', now(), null, null, 1111,
'App\Models\User','');");
});
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
But nothing happens after i send mails using my queue. Should i restart my queue job or do something after modify AppServideProvider?
According to the documentation, JobProcessed is the right event class to use. You might want to update this:
Queue::after(function (JobProcessed $event) {
...
});
By the way, I suggest a bit cleaner approach that leverage the framework better. Laravel has already includes the Illuminate\Mail\Events\MessageSent out of the box. So you can listen to the mail event.

Calling laravel artisan command from console

I am trying to teach myself Laravel command so that later I use it for scheduling them. This is my kernel file:
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
'App\Console\Commands\FooCommand',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('App\Console\Commands\FooCommand')->hourly();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
And this is the command file inside \App\Console\Commands
namespace App\Console\Commands;
use Illuminate\Console\Command;
class FooCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'command:name';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
}
public function fire()
{
$this->info('Test has fired.');
}
}
I want to test the FooCommand command. How do it call this command from shell so that the result is "Test has fired."?
to run your commands manually: php artisan command:name.
Remove your fire function, you can handle this inside handle function.
Fix your schedule function at Kernel class
class Kernel extends ConsoleKernel
{
....
protected function schedule(Schedule $schedule)
{
$schedule->command('command:name')
->hourly();
}
}
To config your schedule please read this:
https://laravel.com/docs/5.4/scheduling

How to get all property for a custom entity in FOSUserBundle?

I am using FOSUserBundle
I created a User entity that extents baseuser, and I added a protected property I called $apiKey. The registration form works fine.
then, I created a userController that extends controller, and in a methoid to edit my user I have:
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')->find($id);
my $user has the apiKey property but this is empty (of course the field in the DB is not).
any idea?
thanks
UPDATE: user entity
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Group")
* #ORM\JoinTable(name="fos_user_user_group",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
/**
* #ORM\Column(type="string", unique=true, nullable=true)
*/
protected $apiKey;
/* public function __construct()
{
parent::__construct();
// your own logic
//$this->roles = array('ROLE_USER'); //default role for new users
}*/
public function __construct()
{
parent::__construct();
// your own logic
$this->groups = new ArrayCollection();
}
/**
* #return mixed
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* #param mixed $apiKey
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
}
/**
* #return mixed
*/
public function getGroups()
{
return $this->groups;
}
/**
* #param mixed $groups
*/
public function setGroups($groups)
{
$this->groups = $groups;
}
}

Event listeners not firing in Laravel 5.1

I am having an issue getting my event listeners to fire in Laravel 5.1.
I am firing the following event:
/**
* Add new project.
*
* #param AddNewProjectRequest $request
* #return Redirect
*/
public function add(AddNewProjectRequest $request)
{
// Event(s);
Event::fire(new ProjectAdded($project, $request->only('file')));
}
I have the following event setup:
<?php
namespace App\Events\Project;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class ProjectAdded extends Event
{
use SerializesModels;
public $project;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($project)
{
$this->project = $project;
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return [];
}
}
Here is my EventServiceProvider file:
<?php
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
\App\Events\ProjectAdded::class => [
\App\Listeners\Project\ImportFileListener::class,
],
];
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
//
}
}
Here is my ImportFileListener listener:
<?php
namespace Woodford\Listeners\Project;
use Woodford\Events\ProjectAdded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class ImportFileListener
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* #param ProjectAdded $event
* #return void
*/
public function handle(ProjectAdded $event)
{
dd('listener');
}
}
As you can see above I have added dd('listener'); to see if the listener gets fired - it doesn't!
I have also tried running php artisan optimize and composer dump-autoload but still no luck!
Does anyone know what could be wrong?

symfony annotations validation override entities/models

I'm trying to override the entities validatation of a forum bundle.
I do it like this:
Category entity:
//src/MSD/ForoBundle/Entity/Category.php
namespace MSD\ForoBundle\Entity;
use Herzult\Bundle\ForumBundle\Entity\Category as BaseCategory;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="Herzult\Bundle\ForumBundle\Entity\CategoryRepository")
*/
class Category extends BaseCategory
{
}
Topic entity:
//src/MSD/ForoBundle/Entity/Topic.php
namespace MSD\ForoBundle\Entity;
use Herzult\Bundle\ForumBundle\Entity\Topic as BaseTopic;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Topic
*
* #ORM\Entity(repositoryClass="Herzult\Bundle\ForumBundle\Entity\TopicRepository")
*
*/
class Topic extends BaseTopic
{
/**
* #ORM\ManyToOne(targetEntity="Category")
*/
protected $category;
/**
* #Assert\NotBlank()
* #Assert\MinLength(limit=4, message="Just a little too short| ")
* #Assert\Regex(
* pattern="/^[a-zA-Z0-9\-_¿?!¡ ]{4,50}$/",
* message="El tema puede contener letras, números, guiones y espacios, interrogantes y exclamaciones. Entre 4 y 30 caracteres"
* )
*/
protected $subject;
/**
* {#inheritDoc}
*/
public function getAuthorName()
{
return $this->author;
}
/**
* #ORM\ManyToOne(targetEntity="User")
*/
private $author;
public function setAuthor(User $user)
{
$this->author = $user;
}
public function getAuthor()
{
return $this->author;
}
}
Post Entity:
//src/MSD/ForoBundle/Entity/Post.php
namespace MSD\ForoBundle\Entity;
use Herzult\Bundle\ForumBundle\Entity\Post as BasePost;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="Herzult\Bundle\ForumBundle\Entity\PostRepository")
*/
class Post extends BasePost
{
/**
* #ORM\ManyToOne(targetEntity="Topic")
*/
protected $topic;
/**
* #Assert\Regex(
* pattern="/^[^<>]{4,1000}$/",
* message="El mensaje no puede contener '<' ni '>'. Entre 4 y 1000 caracteres"
* )
*
*/
public $message;
public function getAuthorName()
{
return $this->author;
}
/**
* #ORM\ManyToOne(targetEntity="User")
*/
private $author;
public function setAuthor(User $user)
{
$this->author = $user;
}
public function getAuthor()
{
return $this->author;
}
}
And the validation works... except the message of the firt post!! that is created when a new topic is created.
I've tried many changes, but without success.
Any idea of why does it happend?
Thank you
Yeah! I got it. The solution was to add this in the Topic entity:
/**
* #Assert\NotBlank
* #Assert\Valid
*/
protected $firstPost;
Then, the message of the first post is validated.

Resources