rhys's avatar
Level 5

Best way to get admin users

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
0 likes
5 replies
Snapey's avatar

i would leave the controller to just deal with the users request, and fire an event that a new record was added

Then in a listener you can deal with 'side-effects' of that record being created such as advising admins

You could fire the event from the controller or you could setup an eloquent model event and do it there. personally i would fire the event in the controller

rhys's avatar
Level 5

@Snapey Thanks for the reply,

I followed the handle sending invoice series for the notification logic so am relatively comfortable with how that's working. I did consider the event / listener documentation due to it being a model creation event I wanted to notify on.

I'm more concerned about best practice for returning all admin users, or returning users that belong to a specific jet stream team. Due to the model being created by non admin user I feel like there is not link to admin users? I'm wondering if I create a Admin class that extends user as demonstrated here? https://tighten.com/blog/extending-models-in-eloquent/

Snapey's avatar

@rhys how do you determine 'admin' status - not with teams?

rhys's avatar
Level 5

@Snapey I'm using a boolean on the user table. Then on the user model have method for checking admin and getting all admins.

Snapey's avatar
Snapey
Best Answer
Level 122

@rhys Just create a scope?

public function scopeAdmin($query)
{
	$query->where('is_admin',1);
}

then when you want to use it

$admins = User::admin()->get();
2 likes

Please or to participate in this conversation.