lk.zachem.info/app/Models/Agent/Agent.php
2025-04-04 14:24:17 +08:00

86 lines
2.0 KiB
PHP

<?php
namespace App\Models\Agent;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Company\Company;
use App\Models\User;
use App\Models\User\UserRole;
use App\Models\User\Role;
use App\Models\Deal\Deal;
use App\Notifications\AgentCreated;
class Agent extends Model
{
use HasFactory;
use SoftDeletes;
public const STATUS_ACTIVE = "ACTIVE";
public const STATUS_DISMISSED = "DISMISSED";
protected $fillable = [
'user_id',
'company_id'
];
public function company()
{
return $this->belongsTo(Company::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function deals()
{
return $this->hasMany(Deal::class);
}
protected static function booted(): void
{
static::created(function (Agent $agent)
{
UserRole::insertOrIgnore([
'user_id' => $agent->user_id,
'role_id' => Role::AGENT
]);
$agent->notify();
});
static::restored(function (Agent $agent)
{
UserRole::insertOrIgnore([
'user_id' => $agent->user_id,
'role_id' => Role::AGENT
]);
$agent->notify();
});
static::updated(function (Agent $agent)
{
if ($agent->trashed())
{
UserRole::where([
'user_id' => $agent->user_id,
'role_id' => Role::AGENT
])->delete();
}
else
{
UserRole::create([
'user_id' => $agent->user_id,
'role_id' => Role::AGENT
]);
}
});
}
private function notify()
{
$this->user->notify(new AgentCreated($this));
return true;
}
}