Hi
I need some help understanding best practice please,
I call a model notification method from the controller when a new model is created, this notifies the user its belongs to model has been created. I then want to fire off a notification to admins that the model is created (so they can review it). I've implemented it as below but I don't think having a static method to return admin users feels right?
controller code in store method
$article->sendArticle($request->user(),$article,"CREATED");
article model method
public function sendArticle(User $user,Article $article,$notificationType)
{
SendArticleNotification::dispatch($user,$article,$notificationType);
}
Notification
class SendArticleNotification implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
protected $article;
protected $notificationType;
protected $adminUsers;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(
User $user,
Article $article,
$notificationType
)
{
$this->user = $user;
$this->article = $article;
$this->notificationType = $notificationType;
$this->adminUsers = User::getAdminUsers();
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if ($this->notificationType == "CREATED")
{
///creation notification to user
$this->user->notify(new ArticleCreated('You have created an Article, this will be reviewed and published shortly',url('/').$this->article->path()));
///creation notification to admin
Notification::send($this->adminUsers,new ArticleCreated('An Article has been created please reveiw and publish',url('/').$this->article->path()));
}
if ($this->notificationType == "PUBLISH")
{
//publish notification to user
$this->user->notify(new ArticleCreated('Your Article has been published',url('/').$this->article->path()));
}
}
}
Static method on user model
public static function getAdminUsers()
{
return User::where('admin', 1)->get();
}
The project does have jetstream and I did consider creating a Publish Article Team but could work out how to implement the below documentation from the notification class on admin users.
// Get all of the team's users, including the owner...
$team->allUsers() : Illuminate\Database\Eloquent\Collection