Laravel Multi-tenant SaaS Module is an eCommerce virtual mall, where multiple vendors can come and sign-up, create their own eCommerce store. Here we are going to discuss on how to make bagisto package compatible with laravel-bagisto saas module.
The merchant doesn’t need to add any plugin to create their store, it is the full end to end integration. You have to just signup then you can start your business.
Make Bagisto Package Compatible with Laravel-Bagisto SaaS Module
Here in this article, we are going to explain step by step on how you can make your Bagisto package compatible with the Laravel Bagisto SaaS Module.
To make your Bagisto package compatible, you have to create another package for SaaS Module. Suppose your package name is PreOrder then create the SaaSPreOrder package.
After this, you have to follow the below steps for making package compatible:
Step 1) Create Migrations For Your Package’s Models
You have to create the migration for all the model’s files of your base package in the new SaaS package (i.e. SaaSPreOrder package).
We have to make a relation between the “companies table” and your package’s tables. For making this possible you have to create the migration for all the tables of your package (i.e. PreOrder package) in the new SaaS package (i.e. SaaSPreOrder package) for adding the “company_id” column in your package table.
php artisan make:migration add_company_id_to_pre_order_items --path=packages/Webkul/SaaSPreOrder/src/Database/Migrations
In the above screenshot, you will see we have created a migration i.e. AddCompanyIdToPreOrderItems in the new SaaS package (i.e. SaaSPreOrder package) to add the “company_id” column in “pre_order_items” table on the below path. Here we defined the company_id column as a foreign key constraint.
php artisan migrate
Create the migration for each of the models with the above command.
Note: If your migration having and unique constraint in the base package (i.e. PreOrder package), then you have to remove that constraint first and after adding ‘company_id’ field you have to add the unique constraint with both the fields i.e. base table unique field and newly added company_id field in the new saas package (i.e. SaasPreOrder package).
Step 2) Extend Base Package Models
After creating migration of all the models for the Saas package (i.e. SaasPreOrder package), You have to extend each model of the base package.
In the above screenshot, We extended the BaseModel (referring PreOrderItem model of base package) in the PreOrderItem model of the SaasPreOrder package.
In the extended model, We have overridden the $fillable property by adding the ‘company_id’ field. For more details regarding $fillable variable follow Laravel Fillable Property
We have also overridden the default query builder by using a newly added field i.e. ‘company_id‘ in where clause. By doing this we do not need to override all default methods of query builder i.e. getAll().
In the overridden newEloquentBuilder method, We used “Company Facade” by which you can call the common methods of the Laravel-Bagisto SaaS Module.
Company::getCurrent()
By using getCurrent method you can get the detail of the current company. Also, you can use the auth()->guard('super-admin')->check()
guard to make the difference between the Super Admin and Normal Admin.
Step 3) Override Package’s Models
After extended all the models in the new Saas package (i.e. SaasPreOrder package), You have to override each model of the base package with the newly created model of Saas package (i.e. SaasPreOrder package).
For making this possible you have to register your models in the “NewSaasServiceProvider.php” file of your SaasPreOrder package.
You have to define a method i.e. overrideModels and call it from the boot(Router $router) method of your ServiceProvider.php. For more detail regarding Laravel ServiceProvider.
You will see, in the above screenshot, we registered the model contracts Contracts\PreorderItem of the base package with the newly created model under the overrideModels method declaration.
Step 4) Create Observers To Listen To Model Events
In the Saas package, we will use Laravel Observers to listen to the multiple events of overridden models. For this, you have to create observer classes to listen to the event of overridden models in your new SaasServiceProvider.php class.
In the above screenshot, you can see we used the overridden model i.e. PreOrderItems of SaasPreOrder package and passed to the creating method as a parameter.
Note: You can use below methods of an observer class: created, updated and deleted
To register an observer you have to use the observe() method on the model which you want to observe.
\Webkul\SaasPreOrderPackage\Models\PreOrderItems::observe(\Webkul\SaasPreOrderPackage\Observers\PreOrderItemsObserver::class);
You have to register your observers to the boot() method of your package’s service provider class i.e. SaasPreOrderServiceProvider.php.
You can take reference regarding Laravel Observer by following this Link.
After making these changes run the below commands from Bagisto Root Directory:
$ composer dump-autoload
Stay tuned for more updates on package development. If you have any issue feel free to raise a ticket at https://bagisto.uvdesk.com/en/