Appearance
Theory behind the package
Actions in Traits
All CRUD Operations for Offices and Office Types are seperated in two traits, so that these actions can be reused in Controllers, Containers & Service Providers. The available Traits, which provide actions are listed below:
Wovosoft\BkbPackages\Traits\HasOfficeCrudstore: Method to create new Office Recordupdate: Method to update existing Office Recordindex: Method to paginate Office Records with Datatable featuredestroy: Method to destroy an existing Office Recordoptions: Method to provide data as options to be used in frontend dropdowns/list.
Wovosoft\BkbPackages\Traits\HasOfficeTypeCrudstore: Method to create new Office Type Recordupdate: Method to update existing Office Type Recordindex: Method to paginate Office Type Records with Datatable featuredestroy: Method to destroy an existing Office Type Recordoptions: Method to provide data as options to be used in frontend dropdowns/list.offices: Method to provide list of offices of a certain OfficeType.
Usage of Actions
1. In Controllers
Now, to make these actions available in Controllers, need to inject these traits as like any other trait in Controller classes.
use \App\Http\Controllers\Controller;
use \Wovosoft\BkbOffices\Traits\HasOfficeCrud;
class OfficeController extends Controller{
use HasOfficeCrud; // NOTE this line
}
use \App\Http\Controllers\Controller;
use \Wovosoft\BkbOffices\Traits\HasOfficeTypeCrud;
class OfficeTypeController extends Controller{
use HasOfficeTypeCrud; // NOTE this line
}
Now, OfficeController has all the methods of HasOfficeCrud trait and OfficeTypeController has all the methods of HasOfficeTypeCrud trait.
In any case, you need to modify the behavior of the default methods, just override those methods.
2. In Container
The actions provided by the above two describing traits, are also available in Wovosoft\BkbOffices\BkbOffices class. This class can be constructed when need to have the CRUD options for offices and types.
To make the above task simple and quicker, Wovosoft\BkbOffices\Facades\BkbOffices facade class provides a singleton of Wovosoft\BkbOffices\BkbOffices. In most cases you should use Wovosoft\BkbOffices\Facades\BkbOffices facade class.
Wovosoft\BkbOffices\Facades\BkbOffices facade provides two methods offices & officeTypes to have the methods of HasOfficeCrud trait and HasOfficeTypeCrud respectively.
use Wovosoft\BkbOffices\Facades\BkbOffices;
BkbOffices::offices()->store($request);
BkbOffices::offices()->index($request);
BkbOffices::offices()->destroy(1);
BkbOffices::officeTypes()->store($request);
BkbOffices::officeTypes()->index($request);
BkbOffices::officeTypes()->destroy(1);
The above example is applicable when you have customized version of Office & OfficeType Controllers.
Default Controllers
To, register routes automatically, the package needs Controllers. So, the package have two default Controllers.
Wovosoft\BkbOffices\Controllers\OfficeController::class;
Wovosoft\BkbOffices\Controllers\OfficeTypeController::class;
Now, you can extend these classes or use Traits to have the CRUD actions in your controllers. Choice is yours. But to me, using traits is much better.
Models of Two Types
The package uses only one table for all type of offices and another table for office types. Office Types are enum values. But, we know that php's enums are key=>value paired. We can't hold extra information or have ORM feature with enums. We need models in that case. So, OfficeType model is created for office types.
So, type # 1 of model is :
Wovosoft\BkbOffices\Models\OfficeTypeWovosoft\BkbOffices\Models\Office
...and to easily categorized different types of Offices the package has the following models:
Wovosoft\BkbOffices\Models\DivisionalOfficeWovosoft\BkbOffices\Models\CrmRmOfficeWovosoft\BkbOffices\Models\BranchWovosoft\BkbOffices\Models\CorporateBranchWovosoft\BkbOffices\Models\DivisionalAuditOfficeWovosoft\BkbOffices\Models\RegionalAuditOfficeWovosoft\BkbOffices\Models\HeadOffice
Note, the above models of type # 2, are fake models. These models use same table ofWovosoft\BkbOffices\Models\Office. The models of type # 2 really helps to have all the features of ORM.
Relations among different models
DivisionalOfficehasMany(CrmRmOffice):crmRmOfficesCrmRmOfficehasMany(Branch):branches
DivisionalAuditOfficehasMany(RegionalAuditOffice):regionalAuditOfficesHeadOffice
The above relations has vice-versa relationship defined.
These above relations among different models, helps to easily offices in tree structure.