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

Veltix's avatar

How I can associate 3 tables with each other

How I can associate business with employees and owner (User table but using roles like Admin/Owner/Manager/Employee) and branches?

    public function getIsAdminAttribute() {
        return $this->hasRole('administrator');
    }
    
    public function getIsOwnerAttribute() {
        return $this->hasRole('owner');
    }
    
    public function getIsManagerAttribute() {
        return $this->hasRole('manager');
    }
    
    public function getIsEmployeeAttribute() {
        return $this->hasRole('employee');
    }

So I can know who is owner of business and all employees. And associated branches.

Scopes:

    public function scopeAllAdministrators() {
        return $this->whereHas('roles', function ($query) {
            $query->where('name', 'administrator');
        });
    }
    
    public function scopeAllCustomers() {
        return $this->whereHas('roles', function ($query) {
            $query->where('name', 'customer')->withoutGlobalScopes();
        });
    }
    
    public function scopeOtherThanCustomers() {
        return $this->whereHas('roles', function ($query) {
            $query->where('name', '<>', 'customer');
        });
    }
    
    public function scopeAllEmployees() {
        return $this->whereHas('roles', function ($query) {
            $query->where('name', 'employee');
        });
    }

Mysql: https://pastebin.com/uNFtfmmK

What Im trying to do is that I have single vendor system and I want to create multivendor so why I need associate these tables so I can know who is business owner and who are his employees and which branches are associated with that business.

0 likes
0 replies

Please or to participate in this conversation.