How I can associate 3 tables with each other

  laravel, mysql, php

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

Source: Laravel

Leave a Reply