Multitenancy in Laravel: Migrations, Testing, and switching tenants

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 พ.ย. 2024

ความคิดเห็น •

  • @bernardonigbinde
    @bernardonigbinde 3 ปีที่แล้ว +4

    Thank you so much! This is the most important Laravel video I’ve ever watched.

  • @juhavehnia7862
    @juhavehnia7862 2 ปีที่แล้ว +4

    Great video! There aren't too many great videos about multi-tenancy and how to build one from scratch. I would love to see part two that looks at making rest of the Laravel tenant specific like you mention the console commands.

  • @OsamaQarem
    @OsamaQarem 4 ปีที่แล้ว +3

    I don't do backend or even PHP, I just enjoy learning from your videos

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      Glad you do :)

  • @NathanBudd
    @NathanBudd 4 ปีที่แล้ว +1

    Wow! This is the best explaination I've seen of this!
    The scenario I've come accross is that all users login at the same url, with the users table in the Landlord database. The user can only belong to one tenant, and so has a tenant_id on the user.

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      Interesting! So the users table is in Landlord connection but each tenant on a separate database?

    • @andjsch
      @andjsch 4 ปีที่แล้ว

      If the login uses emails, the domain could be retrieved from that one as well.

    • @NathanBudd
      @NathanBudd 4 ปีที่แล้ว

      Mohamed Said yes. They don't have different domains or subdomains.

  • @narayanramchandani106
    @narayanramchandani106 4 ปีที่แล้ว +2

    You Sir, are a gentleman and a scholar - thank you for posting this very helpful video!

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      You are very welcome :)

  • @TareqHasan
    @TareqHasan 4 ปีที่แล้ว +6

    Thank you Said for talking about Multinenancy. Would love more videos from you on scaling Laravel.

    • @themsaid
      @themsaid  4 ปีที่แล้ว +4

      Glad you liked it :) I'm sure I'll cover that at some point.

    • @landiman
      @landiman 4 ปีที่แล้ว

      @@themsaid Please! Don't know how to thank you for this. Vapor is a solution for scalling only if tenancy is easy to achive, right?. Can't do it with the current docs...

  • @kedotdev
    @kedotdev 4 ปีที่แล้ว +3

    I had some difficulties running the worker. In config/queue.php I found
    'database' => [
    'driver' => 'database',
    'table' => 'jobs',
    'queue' => 'default',
    'retry_after' => 90,
    ],
    and added:
    'connection' => 'landlord',
    Now everything is going into a central queue instead of independent tenant queues, and I can process all items in the queue via `php artisan queue:work --tries=1` -- besides that, everything was really straight forward to port over. Thanks again!

  • @konstantinkrumov8726
    @konstantinkrumov8726 ปีที่แล้ว

    Amazing video 🙌🏻🙌🏻🙌🏻. Thank you so much !!! Really clear and useful. There is so much to learn from here. All the different functions and config structures. Very well explained and demoed 🙏🏻🙏🏻🙏🏻

  • @alemran7480
    @alemran7480 3 ปีที่แล้ว +1

    Thank you so much for the video. So useful for me. I am developing a SAAS application.

  • @ryangjchandler
    @ryangjchandler 4 ปีที่แล้ว +3

    Great video Mohamed! All very well put, it will definitely help to ease the overcomplicated approaches that people are using.

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      Glad you found it useful :)

  • @kyjan
    @kyjan 4 ปีที่แล้ว

    One approach I took is to use table prefixes for tenants. This saved me some provisioning work when it comes to creating new databases while onboarding a new tenant.

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      That's an interesting approach. It's not common though no sure why :)

  • @rodolfo-w3
    @rodolfo-w3 2 ปีที่แล้ว

    Thank you so much for your video, I needed to learn about this, I really appreciate your dedication. Hug from Brazil

  • @kevinbatdorf
    @kevinbatdorf 4 ปีที่แล้ว +1

    Great video. Great explanation. And very complete. Thanks for including the testing too.

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      Glad you think so :)

  • @nguyenvuuc3758
    @nguyenvuuc3758 ปีที่แล้ว

    you saved my life! thanks a lot for this tutorial

  • @salmankashfy
    @salmankashfy 2 ปีที่แล้ว

    Thank you so much bro. You saved a life.

  • @ahmedhdeawy
    @ahmedhdeawy 4 ปีที่แล้ว +1

    Very thanks, I learning from your videos a lot

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      Glad you are :)

  • @eoina
    @eoina 4 ปีที่แล้ว +1

    Excellent video! So informative!

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      Glad you found it useful :)

  • @guillzf
    @guillzf 4 ปีที่แล้ว +10

    would be great if you make a complete course laravel - tenant from scratch
    Thank you

  • @ahmedkandil505
    @ahmedkandil505 4 ปีที่แล้ว

    Very helpful Waiting for more videos 💓

  • @MrEduarguzher
    @MrEduarguzher 4 ปีที่แล้ว

    Awesome!! Very nice approach

  • @manishpanchal8185
    @manishpanchal8185 8 หลายเดือนก่อน

    Hello Sir! Thank you so much! It's too easy to convert an existing app with this. But how can we manage it in a single domain? I don't want to add domains like you explain.

  • @vic_casanas
    @vic_casanas 4 ปีที่แล้ว +1

    wow, great video man! thanks you explained vey well

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      Glad it helped!

  • @jainsiddharth21
    @jainsiddharth21 6 หลายเดือนก่อน

    Hi, Great tutorial. I've used Tenancy with Multi Tenant & single DB. I need a functionality to ghost login my admin as a tenant user. Could you help me out with that please, if possible.

  • @jonashansen2512
    @jonashansen2512 4 ปีที่แล้ว +1

    Thanks for keeping things simple :)

  • @ajit080260
    @ajit080260 4 ปีที่แล้ว +1

    Thanks a lot for sharing this right on time :) this answer lots of queries and gives better clarity on implementation.
    I would love to see more of such videos.
    Wish audio was bit smooth !

  • @Khalyomede
    @Khalyomede 3 ปีที่แล้ว

    Super interesting thanks for this one!

  • @andreaverrecchia3907
    @andreaverrecchia3907 4 ปีที่แล้ว +1

    How i set dynamically configuration for every tenant?

  • @305020158
    @305020158 2 ปีที่แล้ว

    Each customer requires different customisation to the application, we can ofcourse provide shared db and compute, but , how do i have different application versions for different customers.? Eg : one customer wants normal calculator, one customer wants, scientific calculator, one more customer wants a music player while performing calculations, and one more customer wants voice based calculator

  • @belalsalas7266
    @belalsalas7266 4 ปีที่แล้ว

    Great video, Thank you Mohamed!

  • @kedotdev
    @kedotdev 4 ปีที่แล้ว

    @Mohamed, You mentioned you prefer a tenant structure as tablename_tenantKey rather than an individual database for each tenant. Can we discuss why? I've been learning towards migrating my tenants into their own database, but I'm not sure if I'm ready to make the jump yet. I'll need to figure out a new solution on database backups, etc.
    I'm also feeling like Postgres will be easier to scale as compared to MYSQL. Digital Ocean has much more support for Postgres in regards to real-time backups, failover, etc. What do you think?
    Where can we go from here? I'd love some more content! Thank you.

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      I wrote more on single vs. multi database approaches here divinglaravel.com/multi-tenancy-in-laravel Check it out and let me know if that answers some of your questions

  • @kennedymwenda3357
    @kennedymwenda3357 4 ปีที่แล้ว

    Seems we think alike,used almost the same approach but tenant manager is a separate project since it includes the product website. I Use middleware to switch tenants.

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      Putting the admin in a separate app is a common approach yes.

  • @taslimsuman
    @taslimsuman 4 ปีที่แล้ว +1

    Going over the head, need to watch again and again

  • @projectbased
    @projectbased 3 ปีที่แล้ว +3

    Hi Muhammad, Thanks for putting this together. Nice Work. I have question from the forum. I never developed Saas with multi tenancy before. Should we start this setup from the start of a project or after making our full application for single tenant the we can add these pieces to make it Multitenant? I have an application for one School for example can i turn it now to Multi Schools with unique subdomain?

  • @ladenbin
    @ladenbin 3 ปีที่แล้ว

    bro youre insane thankyou so much

  • @kingshark8848
    @kingshark8848 4 ปีที่แล้ว

    This is my urgent need!! Thanks

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      Happy it helped :)

  • @andiputra6377
    @andiputra6377 4 ปีที่แล้ว

    thanks a lot, you save my days, love it

  • @krishangopal2338
    @krishangopal2338 3 ปีที่แล้ว

    can you please point me to show we will be running scheduled jobs on all tenants?

  • @queopiusa.santos8262
    @queopiusa.santos8262 3 ปีที่แล้ว

    Excellent video 👋🏽, I would like to know how to create the multi database with docker (sail). Thanks for the dedication!

  • @matejsvajger7585
    @matejsvajger7585 4 ปีที่แล้ว +1

    Thanks Said! Great video! It's hard to find information on setting up multi database tenancy with laravel. This was a welcome adddition.
    I'm still missing what to do with Redis and Filesystem in that case. Is there a simple way for prefixing those without extending the existing laravel drivers?

    • @BenClaar
      @BenClaar 4 ปีที่แล้ว

      For Redis, we just edit `/config/database.php`, and set the `database.redis.default.database` config option to a dynamic `$tenantRedisDb` variable. This separates each tenant's redis database.
      For the filesystem, the easiest method is to either use a separate folder or S3 bucket per tenant (setup in /config/filesystems.php), or have a subfolder-per-tenant that you consistently use.

    • @matejsvajger7585
      @matejsvajger7585 4 ปีที่แล้ว

      @@BenClaar Yes, that's clear to use subfolders, but how to prefix them automatically, so you don't need to worry about it in the codebase, for when for example you're resolving the filesystem from the container?
      For redis, I meant when using it as a Cache driver, wouldn't info bleed through to other tenants without some additional prefix?
      Sorry for not beeing clear.

  • @ghulammujtaba3191
    @ghulammujtaba3191 ปีที่แล้ว

    Great video. Can we run it on localhost server? If can then how to run it?

  • @batsmanist
    @batsmanist 5 หลายเดือนก่อน

    Thanks for this.

  • @amolcharpe7302
    @amolcharpe7302 4 ปีที่แล้ว

    I want to create a multitenant application where a new Database should be created when a user is registered, can you please share more details in the repository.

  • @YGNCode
    @YGNCode 4 ปีที่แล้ว

    Very helpful. Thanks Mohamed

  • @alexios4392
    @alexios4392 4 ปีที่แล้ว

    Can you check in code which tenant is used currently? For custom logic for each tenant?

  • @krishangopal2338
    @krishangopal2338 3 ปีที่แล้ว

    No doubt it solves the problem but it leaves me to think about how it will run scheduled jobs in all tenants?

  • @Ramikelesli
    @Ramikelesli 3 ปีที่แล้ว

    if I have complete project done and I want use multi tenancy using subdomain to switch db what the best approach

  • @ahmadmayahi
    @ahmadmayahi 4 ปีที่แล้ว

    Salam Mohammad,
    Changing the database connection using the `config` helper works like a charm, so, I'm wondering what is the purpose of using "DB::reconnect" and "Schema::connection", though?

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      Because the connection is cached in Laravel so we need to purge the connection and re-connect with the new configuration.

  • @mortezarajabi9676
    @mortezarajabi9676 4 ปีที่แล้ว

    Thanks Mohamed,
    I did it before, but not as clean as you.
    How can we have horizon on multi-database approach? To see which job is for which tenant?

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      You can add a tag to jobs that contains the tenant ID so you can figure which tenant a job belongs to.

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      Or you can see that in the job payload if you add it to the payload like in the video.

  • @amolcharpe7302
    @amolcharpe7302 4 ปีที่แล้ว

    Can you or someone please share more info on how to setup and run the application you have shared on github?

  • @pasanmilinda3132
    @pasanmilinda3132 3 ปีที่แล้ว

    How to declare tenant api routes?can you give me a example please

  • @khageshoffice2081
    @khageshoffice2081 4 ปีที่แล้ว

    Thanks for sharing your effort. I have followed your instruction even tried your repository but I am getting 404 error on homepage when I run php artisan serve. Please anyone help.
    Thank you

  • @sirius8ly
    @sirius8ly 2 ปีที่แล้ว

    Great vid. Can you tell me why I might be getting this error. Illuminate\Database\QueryException
    SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')
    I see in the db tenants table database laravel and paravel so I don't know why it's complaining. thanks

    • @sirius8ly
      @sirius8ly 2 ปีที่แล้ว

      Here I realized that you need to manually create those two database laravel and paravel for this to work.

    • @peterkimani2817
      @peterkimani2817 2 ปีที่แล้ว +1

      Adding "DB::statement("CREATE DATABASE IF NOT EXISTS $this->database")" in the Tenant.php file inside the configure() function just before the config array worked for me.

  • @JacobChenn
    @JacobChenn 3 ปีที่แล้ว

    Great video! Thank you

  • @anasfarih
    @anasfarih 4 ปีที่แล้ว

    Great tuto Thank's for explanation

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      Glad it was helpful!

  • @stefanosgiannakis7151
    @stefanosgiannakis7151 3 ปีที่แล้ว

    Great content 👍

  • @MehdiAroui92
    @MehdiAroui92 4 ปีที่แล้ว

    Great video mohammed, thanks

  • @mubafaw
    @mubafaw 10 หลายเดือนก่อน

    Many thanks 😊👍

  • @SethSetiadha
    @SethSetiadha 2 ปีที่แล้ว

    Hi, do you have tips how to maintain for multiple laravel projets that share the base codes but each project has some little customizations, so that when someone update the base codes the other projects will get this updates too?

    • @pratikpawar05
      @pratikpawar05 ปีที่แล้ว

      Hi, did you get the solution for this?

  • @sohelmia1999
    @sohelmia1999 4 ปีที่แล้ว

    Thanks for this video.

  • @godwindaniel845
    @godwindaniel845 2 ปีที่แล้ว

    Hello, Great video. my question is around context switching.. imagine switching db at a very high rate. would this impact performance ?

    • @KashifAmin-AskAmin
      @KashifAmin-AskAmin ปีที่แล้ว

      i have also this question , will handle high traffic

  • @ashok_bishnoi
    @ashok_bishnoi 4 ปีที่แล้ว

    What if all tenant migrations are stored in Migrations/Tenant folder, in this case how migrations will work? I tried and command just ignored the path and migrated all tables to the main connection.

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      What command did you run?

    • @ashok_bishnoi
      @ashok_bishnoi 4 ปีที่แล้ว

      tenants:migrate command and passed client id as argument, and inside command class after checking if seed argument supplied or not, I added one option of path and value of this path was 'migrations/client' but at the end nothing happened in client's database, instead all tables dropped and migrated back in main database.

    • @ashok_bishnoi
      @ashok_bishnoi 4 ปีที่แล้ว

      @@themsaid gist.github.com/kingashok29/b0450269840f2ff404810edc18118a30

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      @@ashok_bishnoi You need to pass the absolute path for the migrations.

    • @ashok_bishnoi
      @ashok_bishnoi 4 ปีที่แล้ว

      @@themsaid Not working even after providing absolute part --path=database/migrations/client

  • @intolap9371
    @intolap9371 4 ปีที่แล้ว

    I followed each and every step and could reach the end quite smoothly, very detailed demonstration. But when I refresh the front end it gets me 404 error laravel.intolap.com/romegasoftware/. Please ignore the directory name. Any relevance?

  • @SouravTah
    @SouravTah 3 ปีที่แล้ว

    How do you add not equal to sign in 7:43 of this video?

  • @mrcombal
    @mrcombal 4 ปีที่แล้ว +2

    Thank you for the huge efforts you put to make development using Laravel much more easy and fun process.
    I have a question here: what do you mean by having to make a database per tenant? Can you please share some scenarios where using a database per tenant would be a better option?
    Thanks again Mohammed

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      He Wessam, Glad it helps :)
      I wrote my thoughts on this in a recent blog post, you can find it here: divinglaravel.com/multi-tenancy-in-laravel

  • @realtorview5024
    @realtorview5024 4 ปีที่แล้ว +1

    Awesome video bro

  • @arun8342
    @arun8342 4 ปีที่แล้ว

    What is the recommended way to run a specific business logic and migration for one tenant?

  • @DanielGamboaStewart
    @DanielGamboaStewart 4 ปีที่แล้ว

    Excelent video.... I am having some problems with Laravel 8 and running my ExampleTest.php i am getting an undefined factory() error. Has anyone run into this problem?
    Tests\Feature\ExampleTest::itReturnsCurrentTenantAndListOfItsUsers
    Error: Call to undefined function Tests\Feature\factory()

    • @renwar
      @renwar 3 ปีที่แล้ว

      In Laravel 8, factory() helper is removed.
      However I am facing another problem which is
      PHPUnit\Framework\InvalidArgumentException: Argument #2 of PHPUnit\Framework\Assert::assertCount() must be a countable or iterable
      and this is the code I get the error from "$response->assertJsonCount(4, 'users');"

    • @renwar
      @renwar 3 ปีที่แล้ว

      I have just fixed my issue. It was from the route, make sure to use \App\Models\User instead \App\User

    • @DanielGamboaStewart
      @DanielGamboaStewart 3 ปีที่แล้ว

      @@renwar what was factory replaced with? in any case i can take a look at your problem over the weekend. I'll reply if i am able to reproduce and fix your problem.

  • @delamberty
    @delamberty 3 ปีที่แล้ว

    Thank you for the video, is this compatible witih Laravel 8?

  • @kennedyosaze1184
    @kennedyosaze1184 4 ปีที่แล้ว

    Great work Mohamed.
    Just have one question. Did you create all the databases (for the tenants) manually before you run the "tenants:migrate" command or it was generated by the same command.

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      I created these manually yes. But you might want to automate that if you allow tenants to sign up to your app.

    • @ajayinurudeen2269
      @ajayinurudeen2269 4 ปีที่แล้ว +1

      @@themsaid Any suggestion on how this can be achieved?

    • @sstchutch
      @sstchutch 4 ปีที่แล้ว

      Interested in your approach to do this automatically as well. I created an event that is fired on sign up.

  • @ahmmedafzal940
    @ahmmedafzal940 4 ปีที่แล้ว +1

    First of all i would like to appreciate your effort Mohamed Said. Let me tell u, i am getting error
    Symfony\Component\Debug\Exception\FatalThrowableError : syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')'
    at D:\xampp\htdocs\multi_tenancy\app\Console\Commands\TenantsMigrateCommand.php:31
    );
    } else {
    Tenant::all()->each(
    fn($tenant) => $this->migrate($tenant)
    );
    }
    }

  • @tarcisiobfilho
    @tarcisiobfilho 4 ปีที่แล้ว

    Mohamed Said, Great video thanks

  • @sodeepnext
    @sodeepnext 2 ปีที่แล้ว

    شكرا محمد في ممكن شروحات بالعربية من فضلك

  • @heshamabdallah8576
    @heshamabdallah8576 4 ปีที่แล้ว

    well done bro!

  • @mokhosh
    @mokhosh 4 ปีที่แล้ว

    You forgot your Screencast TODOs man, that font size...

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      I made the checklist because I forgot the font size in this video :)

    • @mokhosh
      @mokhosh 4 ปีที่แล้ว

      @@themsaid Oh, right. Awesome work man. Keep it up.

  • @cali4484
    @cali4484 4 ปีที่แล้ว

    it took me 1:35 second to hit the like button 😅

  • @eneessylmz
    @eneessylmz 4 ปีที่แล้ว

    Hello, I have a problem. I made a fully working multi-site system. However, the system does not work when the boostrap / cache / config file is created.
    Because config.php file is created for only 1 site. How can I make this file a separate file for each site. I couldnt find it. Please help me.

  • @atoz4541
    @atoz4541 3 ปีที่แล้ว

    multi tenancy spatie route slugs problem -
    Route::domain('{tenant}.localhost')->middleware('tenant')->group(function(){
    Route::prefix('owner')->middleware(['auth', 'owner'])->group(function () {
    Route::get('/Entries/edit/{id}',function($id){
    echo $id;
    });
    });
    });
    output gives tenant name

  • @priyalpatel9949
    @priyalpatel9949 2 ปีที่แล้ว

    Nice Video. But i am having an issue when running with swoole (laravel-s).
    f (!$this->app->runningInConsole()) returns true in TenancyProvider. How can i solve it, any suggestions please?I am using Lumen 8

  • @djoudi5
    @djoudi5 4 ปีที่แล้ว

    Not Found :github.com/themsaid/multitenancy-example

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      Fixed that

  • @zubair.mohsin
    @zubair.mohsin 4 ปีที่แล้ว

    Repo is still private I guess.

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      Fixed that now :)

    • @themsaid
      @themsaid  4 ปีที่แล้ว

      Fixed that. Sorry guys :D

  • @pedroarmandotafurgarcia
    @pedroarmandotafurgarcia 4 ปีที่แล้ว

    excelente vídeo;yo lo hice con múltiples schemas misma base de datos postgresql guiándome del articulo que escribiste divinglaravel.com/understanding-how-laravel-configures-database-connections

  • @mustafael-gaml2235
    @mustafael-gaml2235 4 ปีที่แล้ว

    ليه مش بتنكلم عربي ؟

    • @themsaid
      @themsaid  4 ปีที่แล้ว +1

      علشان أوصل لعدد أكبر :)

    • @mustafael-gaml2235
      @mustafael-gaml2235 4 ปีที่แล้ว

      @@themsaid ان شاءا لله هتوصل لي المليون انت استمر