Appearance
Wovosoft\BkbOffice\Models\Office
Title | Description |
---|---|
uses table | offices |
Purpose | This table is used to hold the records of all type of offices. Eg. Divisional Offices, CRM/RM Offices, Branches etc. |
Types | Different type of Offices are differentiated by type field which is alias of the enum Wovosoft\BkbOffices\Enums\OfficeTypes |
A short discussion
There are seven types of offices of Bangladesh Krishi Bank. These are listed below:
- Divisional Offices (GM/DO)
- Chief Regional / Regional Office (CRM/RM)
- Branches
- Divisional Audit Offices
- Regional Audit Offices
- Corporate Branches
- Departments of Head Office
All these offices are separated by their type
attribute. The type
attribute holds only the values of the enum Wovosoft\BkbOffices\Enums\OfficeTypes
. If values other than this enum is provided, then the value won't be acceptable. Using this field
offices are differentiated.
Now, there is another important field parent_id
. This field is responsible for the hierarchy relationship among the Offices. Let's make it clearer: Branches goes under CRM/RM Offices, CRM/RM Offices goes under Divisional Offices. So, a Branch holds the id
of its CRM/RM Office as it's parent_id
and a CRM/RM Office holds the id
of its Divisional Office as it's parent_id
.
That means the hasMany
, belongsTo
, hasOne
etc. relationships among the offices references to the same model. And that is done by the defined relationships parent
and children
. In most cases this is fine, but it creates a little semantic issue. The parent
and children
names are not semantically suited for offices. We need a better solution. This issue is solved by the pseudo
models listed below:
- Wovosoft\BkbOffices\Models\DivisionalOffice
- Wovosoft\BkbOffices\Models\CrmRmOffice
- Wovosoft\BkbOffices\Models\Branch
- Wovosoft\BkbOffices\Models\CorporateBranch
- Wovosoft\BkbOffices\Models\DivisionalAuditOffice
- Wovosoft\BkbOffices\Models\RegionalAuditOffice
- Wovosoft\BkbOffices\Models\HeadOffice
Why pseudo models
We already have offices in Wovosoft\BkbOffices\Models\Office model. So, don't need to apply same thing again in different models. The above pseudo models just let us help to find the offices in a better semantic way.
Implementation of pseudo models
First, we have created global scopes for the seven types of offices. These are listed below:
- Wovosoft\BkbOffices\Models\Scopes\DivisionalOffices
- Wovosoft\BkbOffices\Models\Scopes\CrmRmOffices
- Wovosoft\BkbOffices\Models\Scopes\Branches
- Wovosoft\BkbOffices\Models\Scopes\CorporateBranches
- Wovosoft\BkbOffices\Models\Scopes\DivisionalAuditOffices
- Wovosoft\BkbOffices\Models\Scopes\RegionalAuditOffices
- Wovosoft\BkbOffices\Models\Scopes\HeadOffices
Then each pseudo model uses its corresponding global scope to make the different from others.
Example of Branch Scope
<?php
namespace Wovosoft\BkbOffices\Models\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Wovosoft\BkbOffices\Enums\OfficeTypes;
class Branches implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param Builder $builder
* @param Model $model
* @return void
*/
public function apply(Builder $builder, Model $model): void
{
$builder->where("type", "=", OfficeTypes::Branch);
}
}
Example of Branches scope application in Branch pseodo model
<?php
namespace Wovosoft\BkbOffices\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Wovosoft\BkbOffices\Enums\OfficeTypes;
use Wovosoft\BkbOffices\Models\Scopes\Branches;
class Branch extends Model
{
protected $table = "offices";
protected static function booted()
{
static::addGlobalScope(new Branches);
}
}
Same logic has been applied for the other pseudo models. These pseudo models contains the relationships among different types of offices. Check the source code for more details.
Checking type
There are custom attributes to check the type of Office Type
is_branch
is_corporate_branch
is_div_office
is_dao_office
is_rao_office
is_crm_rm_office
is_head_office