Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Michael Fayez's avatar

How to retrieve data in that case plz help

I would like to retrieve data of Assignee from Project Model in Observer and send them email here is my Project Model :-


namespace App\Models;

use App\Support\HasAdvancedFilter;
use App\Traits\Auditable;
use Carbon\Carbon;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Package extends Model
{
    use HasFactory, HasAdvancedFilter, SoftDeletes, Auditable;

    public $table = 'packages';

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $fillable = [
        'name',
        'ads',
        'post',
        'design',
        'video',
        'platforms',
        'story',
    ];

    public $orderable = [
        'id',
        'name',
        'ads',
        'post',
        'design',
        'video',
        'platforms',
        'story',
    ];

    public $filterable = [
        'id',
        'name',
        'ads',
        'post',
        'design',
        'video',
        'platforms',
        'story',
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getCreatedAtAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
    }

    public function getUpdatedAtAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
    }

    public function getDeletedAtAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
    }
}

here is my Observer to retrieve date and send them email what shall I DO TO SEND FOR ASSIGNEE NOT ADMIN ??

<?php

namespace App\Observers;

use App\Models\Project;
use App\Models\User;
use App\Notifications\DataChangeEmailNotification;
use Notification;

class ProjectObserver
{
    public function created(Project $project): void
    {
        $payload = [
            'action' => 'created',
            'model'  => sprintf('%s#%s', get_class($project), $project->id),
            'reason' => auth()->user(),
        ];

        $admins = User::admins()->get();

        Notification::send($admins, new DataChangeEmailNotification($payload));
    }

    public function updated(Project $project): void
    {
        $payload = [
            'action' => 'updated',
            'model'  => sprintf('%s#%s', get_class($project), $project->id),
            'reason' => auth()->user(),
        ];

        $admins = User::admins()->get();

        Notification::send($admins, new DataChangeEmailNotification($payload));
    }

    public function deleted(Project $project): void
    {
        $payload = [
            'action' => 'deleted',
            'model'  => sprintf('%s#%s', get_class($project), $project->id),
            'reason' => auth()->user(),
        ];

        $admins = User::admins()->get();

        Notification::send($admins, new DataChangeEmailNotification($payload));
    }
}

here is my User Model :-

<?php

namespace App\Models;

use App\Models\UserAlert;
use App\Support\HasAdvancedFilter;
use App\Traits\HasTeam;
use Carbon\Carbon;
use DateTimeInterface;
use Hash;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements HasLocalePreference, MustVerifyEmail
{
    use HasFactory, HasAdvancedFilter, Notifiable, HasTeam, SoftDeletes;

    public $table = 'users';

    protected $casts = [
        'is_approved' => 'boolean',
    ];

    protected $hidden = [
        'remember_token',
        'password',
    ];

    protected $dates = [
        'email_verified_at',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $fillable = [
        'name',
        'email',
        'password',
        'locale',
        'team_id',
        'is_approved',
    ];

    public $orderable = [
        'id',
        'name',
        'email',
        'email_verified_at',
        'locale',
        'team.name',
        'is_approved',
    ];

    public $filterable = [
        'id',
        'name',
        'email',
        'email_verified_at',
        'roles.title',
        'locale',
        'team.name',
    ];

    public function getIsAdminAttribute()
    {
        return $this->roles()->where('title', 'Admin')->exists();
    }

    public function scopeAdmins()
    {
        return $this->whereHas('roles', fn ($q) => $q->where('title', 'Admin'));
    }

    public function alerts()
    {
        return $this->belongsToMany(UserAlert::class)->withPivot('seen_at');
    }

    public function preferredLocale()
    {
        return $this->locale;
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getEmailVerifiedAtAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
    }

    public function setEmailVerifiedAtAttribute($value)
    {
        $this->attributes['email_verified_at'] = $value ? Carbon::createFromFormat(config('project.datetime_format'), $value)->format('Y-m-d H:i:s') : null;
    }

    public function setPasswordAttribute($input)
    {
        if ($input) {
            $this->attributes['password'] = Hash::needsRehash($input) ? Hash::make($input) : $input;
        }
    }

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    public function getCreatedAtAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
    }

    public function getUpdatedAtAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
    }

    public function getDeletedAtAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
    }

    public function team()
    {
        return $this->belongsTo(Team::class);
    }
}

Please help :) would like to send the Assignee people email beside the admin not only the admin

0 likes
2 replies
LaryAI's avatar
Level 58

To send an email to the assignee of a project in the ProjectObserver, you can modify the created, updated, and deleted methods as follows:

public function created(Project $project): void
{
    $payload = [
        'action' => 'created',
        'model'  => sprintf('%s#%s', get_class($project), $project->id),
        'reason' => auth()->user(),
    ];

    $assignee = $project->assignee; // Assuming there is an "assignee" relationship in the Project model

    Notification::send($assignee, new DataChangeEmailNotification($payload));
}

public function updated(Project $project): void
{
    $payload = [
        'action' => 'updated',
        'model'  => sprintf('%s#%s', get_class($project), $project->id),
        'reason' => auth()->user(),
    ];

    $assignee = $project->assignee; // Assuming there is an "assignee" relationship in the Project model

    Notification::send($assignee, new DataChangeEmailNotification($payload));
}

public function deleted(Project $project): void
{
    $payload = [
        'action' => 'deleted',
        'model'  => sprintf('%s#%s', get_class($project), $project->id),
        'reason' => auth()->user(),
    ];

    $assignee = $project->assignee; // Assuming there is an "assignee" relationship in the Project model

    Notification::send($assignee, new DataChangeEmailNotification($payload));
}

Make sure to define the "assignee" relationship in the Project model. For example:

public function assignee()
{
    return $this->belongsTo(User::class, 'assignee_id');
}

Replace 'assignee_id' with the actual foreign key column name in the projects table that references the users table.

This will retrieve the assignee of the project and send them an email notification using the DataChangeEmailNotification class.

Michael Fayez's avatar

@LaryAI SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.user_id' doesn't exist

select `users`.*, `user_id`.`project_id` as `pivot_project_id`, `user_id`.`user_id` as `pivot_user_id` from `users` inner join `user_id` on `users`.`id` = `user_id`.`user_id` where `user_id`.`project_id` in (1, 2) and `users`.`deleted_at` is null

Please or to participate in this conversation.