author-img

Mitul Golakiya's Posts

CEO & Founder

post

Recently Spatie released a brand new package for multi-tenancy called laravel-multitenancy.

It comes with great support to work out of the box with sub-domains like,

https://zluck.infychat.com

https://infyom.infychat.com

https://vasundhara.infychat.com

It identified the tenant based on the sub-domain and sets a database runtime for your tenant-specific models.

Recently, we used it in one of our client for Snow Removal CRM. Here we have two models,

  1. Freemium Model - with no sub-domain (application will work on main domain only)
  2. Premium Model - where the tenant will get its subdomain

And user can convert his account from Freemium to Premium at any point of time by just subscribing to the plan.

So what we want is, on the backend, we want to have a separate database for each tenant, but the application should run on main as well as a sub-domain.

So what we want to have is the ability to extend/customize the tenant detection mechanism. And Spatie does a very good job at there where you can customize the logic.

You can create your own TenantFinder class and configure it in the config file of the package. And there is very good documentation for that here: https://docs.spatie.be/laravel-multitenancy/v1/installation/determining-current-tenant/

To do that, what we did is, we have a field called tenant_id in our users table. All of our users are stored into the main database since we may have user access across the tenant.

And when any user does a login, we listen for the event Illuminate\Auth\Events\Login which is documented in Laravel Docs over here.

When a user does a login, our listener will be called and will create a cookie called tenant on the browser with the tenant id of the user. So our listener will look like,

<?php

namespace App\Listeners;

use App\Models\User;
use Cookie;
use Illuminate\Auth\Events\Login;

class LoginListener
{
    public function handle(Login $event)
    {
        /** @var User $authUser */
        $authUser = $event->user;

        Cookie::forget('tenant');
        Cookie::queue(Cookie::forever('tenant', encrypt($authUser->tenant_id)));
    }
}

Also, we encrypt the cookie on our end, so we do not want Laravel to encrypt it again, so we added tenant cookie into except array of EncryptCookies middleware as per documentation here. so our middleware looks like,

<?php

namespace App\Http\Middleware;

use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;

class EncryptCookies extends Middleware
{
    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    protected $except = [
        'tenant'
    ];
}

Now at the last point, we extended our logic to find a tenant and get it to work on the main domain as well as sub-domain.

We have created our own custom class called InfyChatTenantFinder, which looks like,

<?php

namespace App\TenantFinder;

use App\Models\Account;
use App\Models\SubDomain;
use Illuminate\Http\Request;
use Spatie\Multitenancy\Models\Concerns\UsesTenantModel;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\TenantFinder\TenantFinder;

class InfyChatTenantFinder extends TenantFinder
{
    use UsesTenantModel;

    public function findForRequest(Request $request): ?Tenant
    {
        $host = $request->getHost();

        list($subDomain) = explode('.', $host, 2);

        // Get Tenant by subdomain if it's on subdomain
        if (!in_array($subDomain, ["www", "admin", "infychat"])) {
            return $this->getTenantModel()::whereDomain($host)->first();
        }

        // Get Tenant from user's account id if it's main domain
        if (in_array($subDomain, ["www", "infychat"])) {

            if ($request->hasCookie('tenant')) {
                $accountId = $request->cookie('tenant');

                $accountId = decrypt($accountId);

                $account = $this->getTenantModel()::find($accountId);

                if (!empty($account)) {
                    return $account;
                }

                \Cookie::forget('tenant');
            }
        }

        return null;
    }
}

So basically, first we check if the sub-domain is there, then find tenant from the sub-domain.

If the domain is the main domain then get the tenant id from the cookie and return the account (tenant) model.

So this is how you can customize the logic the way you want to have a custom tenant identification system.

July 24, 20203 minutesauthorMitul Golakiya
post

Recently, the Laravel team announced a Laravel Fortify. A framework agnostic authentication backend for laravel applications. It provides registration, authentication along with two-factor authentication.

As said above, it is a framework agnostic, so it doesn't provide any blade views with it. You can implement views of your choice of the frontend. Blade, Vue, React with Bootstrap or TailwindCSS, or any other CSS framework.

Today we are going to see how we can use Laravel Fortify with one of the most popular Bootstrap 4 theme AdminLTE v3.

We can actually do that in minutes with the package that we already developed called Laravel UI AdminLTE.

This package also works with the previous laravel version to have an authentication system with Laravel UI for Laravel Frontend Scaffolding.

Let's see step by step, how we can do that.

Install Packages

Install Laravel Fortify and Laravel UI AdminLTE by the following command,

composer require laravel/fortify infyomlabs/laravel-ui-adminlte

Publish Fortify Resources

This command will publish all required actions in the app/Actions directory along with the Fortify configuration file and migration for two-factor authentication.

php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"

Run Migrations

Then run migrations,

php artisan migrate

Add Fortify Service Provider

Next step, add published FortifyServiceProvider to config/app.php

Run AdminLTE Fortify Command

Run the following command,

php artisan ui adminlte-fortify --auth

Install Node Modules and Run a Build

As a next step, install required npm modules and run a build,

npm install && npm run dev

And we are done. Now visit the home page and you should be able to see the full authentication system working including,

  • Login
  • Registration
  • Forgot Password
  • Reset Password
  • Home page

Laravel AdminLTE UI also provides a starting layout with a sidebar menu and header once you do login. so you are all set to go.

June 27, 20202 minutesauthorMitul Golakiya
post

Sometimes we need to load a large amount of data into memory. Like all the models we have in the database.

For e.g. PDF Printing, Perform some global updates, etc.

So the general practices we use in Laravel is to write the following code,

$users = User::all();

Just imagine I have 10,000 users in the database and when I load all the users in one shot.

But it takes a really high amount of memory to load all the records and prepare Laravel Model class objects. And sometimes we also load them in chunks to save the memory, but in some use cases, chunking can not be the option.

Here is the screenshot of mine when I load 10,000 users into memory with the above code.


10k Models


It's using 37MB memory. Also, imagine the required memory if we are loading some relationships as well with these 10,000 records.

The Eloquent model is a great way to handle operations with lots of features like Mutators, Relationships, and much more.

But we really do not use these features all the time. We simply output or use the direct values which are stored in the table. So ideally, we do not need an eloquent model at all, if we are not going to use these features.

In those cases, Laravel also has a handy function toBase(). By calling this function it will retrieve the data from the database but it will not prepare the Eloquent models, but will just give us raw data and help us to save a ton of memory.

So my revised code will look something like this,

$users = User::toBase()->get();

Check the revised memory screenshot after adding the toBase function.


10k Models toBase


So it almost saves 50% of the memory. It's reduced from 35MB to 20MB and the application also works much much faster, because it doesn't need to spend time in preparing 10,000 Eloquent models.

So if you are not really going to use features of Eloquent and loading a large amount of data, then the toBase function can be really useful.

Here you can find a full video tutorial for the same.

June 21, 20202 minutesauthorMitul Golakiya
post

Recently, I've started working on one project where we follow modules pattern and for the same, we have different assets folder for the different modules and the folder named common for assets which are common across all the modules.

So our public folder looks like the following,

Module Asset Functions

The problem that I started facing was everywhere I need to give a full path to import/include any of the files from any of the folders. For e.g.

<img src="{{ asset('assets/tasks/images/delete.png') }}" alt="Delete Task">

Even if we have some folder in images to group similar images then it was even becoming longer. For e.g.

<img src="{{ asset('assets/tasks/images/social/facebook.png') }}" alt="Facebook">

The workaround that I used is, I created one file called helpers.php and created dedicated asset functions for each of the modules. For e.g., for tasks,

if (!function_exists('tasks_asset')) {
    /**
     * Generate an asset path for tasks module folder.
     *
     * @param  string  $path
     * @param  bool|null  $secure
     * @return string
     */
    function tasks_asset($path, $secure = null)
    {
        $path = "assets/tasks/".$path;
        return app('url')->asset($path, $secure);
    }
}

With this function, I can use,

<img src="{{ tasks_asset('images/delete.png') }}" alt="Delete Task">

Other advantages it gives are,

  1. if in future if the path of tasks folder changed, then I do not need to go and update every single import/include.
  2. I (or any new developer) do not need to remember the long paths and can always use direct function names for modules.

Even I like this pattern so much, so I went further and created dedicated image function as well,

if (!function_exists('tasks_image')) {
    /**
     * Generate an asset path for tasks module images folder.
     *
     * @param  string  $path
     * @param  bool|null  $secure
     * @return string
     */
    function tasks_image($path, $secure = null)
    {
        $path = "images/".$path;
        return tasks_asset($path, $secure);
    }
}

So I can use it as,

<img src="{{ tasks_image('delete.png') }}" alt="Delete Task">

Simple and handy functions to use everywhere.

May 16, 20202 minutesauthorMitul Golakiya
post

In chapter 5 of Gita, Karma-Sanyas-Yog, Lord shree Krishna said in verse 8,

યોગયુક્ત તત્વને જાણનારો મનુષ્ય જુએ, સાંભળે, સ્પર્શ કરે, સૂંઘે, ખાય, ચાલે, ઊંઘે, શ્વાસ લે, બોલે, મળત્યાગ કરે, ગ્રહણ કરે, નેત્ર ઉઘાડે તથા મીંચે તો પણ "ઇન્દ્રિયોના વિષયોમાં ઇન્દ્રિયો વર્તે છે" એમ સમજી "હું કાંઈ જ કરતો નથી" એમ મને છે.

The wise person knows, just like breathing, smelling, opening and closing eyelid, eat, sleep happens automatically, just like that He believes that everything is happening by itself. I’m just doing nothing.

Same in business, do not try to micro-manage things. Some things happen by itself. And let them happen in their own way. If you will spend your time in micro-managing those small things then you will never able to focus on other important things.

The another important thing is,

જ્ઞાનીઓ વિદ્યાવિનયયુક્ત બ્રાહ્મણમાં, ગાયમાં, હાથીમાં, કૂતરામાં તથા ચાંડાલમાં પણ સમાન દ્રષ્ટિ રાખવાવાળા હોય છે.

Sometimes people only respect certain people. Like some people only give respect to rich and financially sound people but they don’t pay attention to middle or lower income group people. Such behavior is not proper. Same way in office as well, we give attention to some people and we might not give attention to the office boy, pun, watchman, cleaner, etc. or Senior Developer vs Junior Developer. One must consider each and everyone with equality. Each and everyone should be respected. Just say them Hi or Hello and see the Joy that you and they get.

You can also listen to the full podcast here.

February 07, 20201 minuteauthorMitul Golakiya
post

In Last Podcast and Blog Post, we discussed how we need to create 4 departments in our business and become A-Karta in our organization.

But just becoming A-Karta doesn't work. So in verse 7 & 8, Lord Shree Krishna said and this is one of the most popular verses of Bhagavad Gita,

यदा यदा हि धर्मस्य ग्लानिर्भवति भारत |

अभ्युत्थानमधर्मस्य तदात्मानं सृजाम्यहम् || 7 ||

परित्राणाय साधूनां विनाशाय च दुष्कृताम् |

धर्मसंस्थापनार्थाय सम्भवामि युगे युगे || 8 ||

હે ભારત! જયારે જયારે ધર્મ ને હાનિ થાય છે અને અધર્મનું જોર વધે છે, ત્યારે ત્યારે હું જન્મ ધારણ કરું છું. સત્પુરુષોનાં રક્ષણ માટે, દુષ્ટોના વિનાશ માટે અને ધર્મની સારી રીતે સ્થાપના કરવા માટે હું યુગે યુગે પ્રકટું છું.

Whenever there is an increase in unrighteousness, Lord appears on earth age by age, to protect the righteous, to annihilate the wicked, and to re-establish the principles of dharma.

Same way when our 4 created departments do not do their work or perform their duties or do not produce desired results, then we also need to come in the picture time by time.

Whenever there is an increase in unrighteousness - People do not perform their duties or you do not get results

Lord appears on earth age by age - You need to come in the picture time by time

protect the righteous, to annihilate the wicked - to protect your culture & company & its people

and to re-establish the principles of dharma - to re-establish the balance and processes of the company.

You can also listen to the full podcast here.

February 01, 20201 minuteauthorMitul Golakiya
post

4 Varnas in Hinduism

Image Source

Shreemad Bhagavad Gita has a lot to speak about business management. In the last Post of Karma-Yoga, we have seen how our actions should be for the benefits of the others. Today, let's see Chapter 4.

In Chapter 4, Karma-Brahm-Arpan-Yoga (Jñāna Karm Sanyās Yog), verse 13th and 14th, Lord Krishna said,

चातुर्वर्ण्यं मया सृष्टं गुणकर्मविभागश: |

तस्य कर्तारमपि मां विद्ध्यकर्तारमव्ययम् || 13 ||

The four categories of occupations were created by me according to people’s qualities and activities. Although I am the creator of this system, know me to be the non-doer and eternal. (source: holy-bhagavad-gita.org)

પ્રકૃતિના ત્રણ ગુણો એન્ડ કર્મોના વિભાગ પ્રમાણે મેં બ્રાહ્મણ, ક્ષત્રિય, વૈશ્ય અને શુદ્ર એમ ચાર વર્ણોની રચના કરી છે. તેનો હું કર્તા હોવા છતાં પણ તું મને અકર્તા અને અવિકારી જાણ.

न मां कर्माणि लिम्पन्ति न मे कर्मफले स्पृहा |

इति मां योऽभिजानाति कर्मभिर्न स बध्यते || 14 ||

Activities do not taint me, nor do I desire the fruits of action. One who knows me in this way is never bound by the karmic reactions of work.(source: holy-bhagavad-gita.org)

કોઈ કર્મો મને લેપતા નથી, કેમ કે કર્મોના ફળમાં મને લાલસા નથી. આ રીતે જે મનુષ્ય મને જાણે છે, તે પોતાના કર્મો વડે બંધાતો નથી.

Based on occupation & activities, God created the following 4 Varnas:

  • Brahmins - predisposed toward teaching and worship (how to get the God or go to heaven)
  • Kshatriyas - inclined toward administration and management of the kingdom
  • Vaishyas - form the business and agricultural class (Businessmen)
  • Shudras - working class (The actual person who works)

In the same way, we also need 4 Departments in our Business.

  • Brahmins - Marketing - Do the marketing & they actually help us to meet our God (Our Customer).
  • Kshtriya - HR - Form a Business, Hire Team, Appoint People, Make Policies, define Penalties, etc.
  • Vaishyas - Operations - Administration & Management (or your Top Management Team).
  • Shudra - Technical - Actual working people, our employees who actually do work

What God did is, created these 4 Varnas and given them their duties and activities that they need to perform and he simply became A-Karta (non-doer and eternal).

We as CEO, also need to do that same thing. Create these 4 Departments, assign them their duties and just become an external observer. Then all you need to do is, whenever they got stuck, help them, guide them or train them. That's it.

So just simply, Be a God of your Company.

You can also listen to full podcast here.

January 24, 20202 minutesauthorMitul Golakiya
post

2019 was a great year for us in terms of business as well as in my personal life. We have some major breakdowns which taught us lots of good lessons while we resolved it.

1. Do not get your company to rely on one major client

The major mistake we made was our 80% staff was working for one client for years. And when that work got stopped the 80% company has no work with 50% staff was a senior and experienced developer with top salaries.

Of course, long term relationship is important but always keep working with multiple clients/people. It will keep your business moving even if one of your work got stopped for whatsoever reason. Because even if you have top talent, getting really good clients is too tough in the market.

2. Everything starts with YOU

Before you transform your business, the first thing that needs to be transformed is yourself. Once you start transforming, everything else into your business, your team will start transforming automatically.

Your team learns a lot from you, they are your main observers. Once they see you transforming, their life will also start getting transformed.

3. Wake up Early

All great leaders are early birds. Once you win your morning, you can win the rest of the day. 3 hours of early mornings are the main productive hours. That’s the time when you can complete your things which you are not able to complete throughout your day or you are not getting time for.

Also waking up at fixed early morning time will help you a lot in your health. Wake up at 5 am at least, do some exercise, reading and complete the most important thing of the day in the morning only.

4. Power of Reading

“Not every Reader is a Leader but every Leader is a Reader.”. This sentence has a lot to say. Once you start reading it will transform your life a lot. The good book contains years of research by the author. So you can directly get those years of knowledge by reading that book in just a few days.

5. Increase your Networking

I will say you should meet lots of people. Meet them, talk with them, know them, learn from them even if you are not working with local people.

Plan to meet one good CEO or Coach every month. Invite them to lunch or dinner and try to learn as much as you can from them. Ask about their processes, structures, systems, technologies, etc.

6. Contribution in other’s lives

When you contribute to other people’s lives, that contribution will come directly back to you by double. When you transform their life, your life will automatically get transformed.

7. Get a Coach or Mentor

Always surround yourself with Coaches and Mentors at every and each stage of life. In Business and Personal life as well. Your coach can be anyone from Friends, Family or some specialized Mentors and Coaches.

You always need someone who can push you, help you, guide you. Get someone who can give your honest and transparent advice and guidance.

8. Never stop Learning

Never stop learning. Always learn something every day or at least a week. Read Books or Watch Videos or Learn new technologies or Purchase some Courses. Even if you are super busy, put a dedicated time to watch 10 mins video or Read 10 mins every day.

In the long run, just 10 mins of every day will help you a lot.

9. Write Daily Journal

Writing your daily Journal helps a lot. List down your today’s achievements and your tomorrow’s goals. It will help you to track your daily progress as well as plan your day of tomorrow.

When you write a Journal, you will have an exact idea on the next day, what are your priorities in the morning only. Once you plan your day ahead a day, it has tremendous benefits.

10. Delegation

Delegation is really powerful. Before doing any task, just think if you can delegate it then delegate it. Your time is limited and precious, put it on the things where it’s actually needed rather than doing things that someone else can do from your team.

And if they are not able to do it right now, start training them and make them capable to take off your load.

11. Power of ToDo List

Start Maintaining To-Do list. It has huge power and will bring a great impact. Anything that you need to do, add to your ToDo list. Need to make a call to a friend? add it. Need to send an Email to the client? Add it.

Each and every small thing should be added to the ToDo list with a due date if possible. It will help you to prioritize your tasks and will give you motivation at the end of the day when you check the list of complete things.

12.Write a Blog

Start writing your blog. It doesn’t need to be fancy or content-rich. Just write down anything that seems to be useful to at least one person in the world. Just write down about things that came to you across the day.

Your blog post doesn’t need to long. Just write 2-3 small paragraphs and that’s it. But at least start writing something daily/weekly whatever is possible for you.

Hope you enjoyed it and see you next year with new learnings.

January 23, 20204 minutesauthorMitul Golakiya