Reading list Switch to dark mode

    How To Make Your Package Compatible With Laravel-Bagisto SaaS Module

    Updated 14 September 2022

    Today we will learn how to make your Laravel development packages compatible with the Laravel Bagisto SaaS Module, you can check the Bagisto SaaS Module feature by following this link.

    To make your 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 the package compatible:

    Step 1) Create Migrations for your package’s Models

    You have to create the migration for all the model 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.

    Laravel-Bagisto SaaS Module

    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.

    Start your headless eCommerce
    now.
    Find out More

    php artisan migrate

    Create the migration for each of the models with the above command.

    Note: If your migration has and unique constraint in the base package (i.e. PreOrder package), then you have to remove that constraint first and after adding the ‘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).

    Laravel-Bagisto SaaS Module

    Step 2) Extend Base Package Models

    After creating the migration of all the models for the Laravel development services Saas package (i.e. SaasPreOrder package), You have to extend each model of the base package.

    Laravel-Bagisto SaaS Module

    In the above screenshot, We extended the BaseModel (referring PreOrderItem model of the 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 the $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 the where clause. By doing this we do not need to override all default methods of the 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 extending 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 the 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.

    Laravel-Bagisto SaaS Module

    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.

    Laravel-Bagisto SaaS Module

    You will see, in the above screenshot, that 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.

    Laravel-Bagisto SaaS Module

    In the above screenshot, you can see we used the overridden model i.e. PreOrderItems of SaasPreOrder package, and passed it to the creating method as a parameter.

    Note: You can use the 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.

    Laravel-Bagisto SaaS Module

    You can take reference regarding Laravel Development Service Observer by following this Link.

    After making these changes run the below commands from Bagisto Root Directory:

    $ composer dump-autoload

    Thanks for reading this article.

    Current Product Version - v0.0.1

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home