UserProjectPivot.php 1010 Bytes
<?php

namespace App\Models\Pivots;

use App\Models\Project;
use App\Models\User;
use App\Support\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UserProjectPivot extends Model
{
    protected $table = 'user_has_projects';

    protected $guarded = [];

    protected static function booted(): void
    {
        static::deleted(static function (UserProjectPivot $projectManager) {
            Project::query()
                ->whereKey($projectManager->getAttribute('project_id'))
                ->where('master_id', $projectManager->getAttribute('user_id'))
                ->update(['master_id' => 0]);
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function project(): BelongsTo
    {
        return $this->belongsTo(Project::class);
    }
}