[Live-Coding] Laravel Multi-Tenancy with Single Trait

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ก.ย. 2024
  • A demo-project to show you how easy it is to add multi-tenancy to Laravel project. First we do that with observers, but then make it more flexible with a trait.
    Project code on Github: github.com/Lar...
    Article version with code snippets: laraveldaily.c...
    - - - - -
    I'm available for freelance Laravel work, check out my Upwork profile: bit.ly/povilas-...
    - - - - -
    Our daily Laravel blog: laraveldaily.com
    Our 50+ Laravel tips in a FREE e-book: bit.ly/laravel-...
    Enroll in our online courses: bit.ly/laravel-...
    Try our QuickAdminPanel code generator: bit.ly/quickadm...
    Follow us on Twitter: bit.ly/twitter-...

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

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

    an important note here is that the trait's boot method name must follow the pattern boot+trait_name in order for the method to be called automatically inside the model

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

    Even though this video is quite old now, it's still one of the best introductions to multi-tenancy for Laravel available. The problem is between 2020 and 2021 everyone went on tangent to try and do fancy stuff with multi-tenancy - new plugins - new packages - multi-data - tenant identification by domain name. The bottom line is for most basic multi-tenancy apps scoping and observers are more than enough.

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

    This is just pure perfection and simplicity for something I needed to implement today. Thank you

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

    Brilliant tutorial. Thanks!
    One thing that caught me out: The boot method suffix MUST match the class name.
    trait MultitenantableTrait {
    public static function bootMultitenantableTrait(). //The method name here is boot{classname}
    ...not
    trait MultitenantableTrait {
    public static function bootMultitenantable() //no 'trait' on the method name
    I know this is probably super obvious to most people, but it took me quite a while to work out what was going on! :-)

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

    Wonderful... Thank you for making this.

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

    Great video, if you want to allow for joins where multiple tables will have the created_by_user_id, you can add this to your trait.
    $builder->where($builder->getModel()->getTable() . '.created_by_user_id', auth()->id);
    Keep up the good work!

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

    Excellent demonstration, thank you so much for this video

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

    You are super awesome, thank you.

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

    YOU ARE A M A Z I N G !!!!!!!! Tks so much, you saved my life

  • @kishanchauhan-ks2hk
    @kishanchauhan-ks2hk 3 ปีที่แล้ว

    nice tutorial

  • @ernesto.pareja
    @ernesto.pareja ปีที่แล้ว

    Thank you for this tutorial!!!

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

    wow, thank you sir

  • @bernardonigbinde
    @bernardonigbinde 5 ปีที่แล้ว +1

    Yep! Definitely useful, and added to my list of things to implement. Thank you!

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

    Awesome! Coool Man. Thank you very much for this tutorial.

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

    Great and simple!! thank you !!

  • @almsafa7
    @almsafa7 5 ปีที่แล้ว +1

    Thank you

  • @jniyaz
    @jniyaz 5 ปีที่แล้ว

    Very Useful. Thanks.

  • @hamzabouk5982
    @hamzabouk5982 5 ปีที่แล้ว +6

    So helpful thank you sooo much. Can you please make one using hyn/multi-tenancy package

    • @LaravelDaily
      @LaravelDaily  5 ปีที่แล้ว +2

      Currently not planning it, because that package assumes multi-database setup which is more complicated and requires more effort.

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

      @@LaravelDaily yes, but that is actually the real multi tenancy--when you run multiple domains on the same codebase.

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

      Alin Mazilu totally agree

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

      I'm going to agree with Povilas here. Multiple database and multiple domains sounds very nice until you start realising the extra effort and complexity. There is now some "nice" packages to do it, but at the end of the day after a while the complexity starts to haunt one.

  • @GrupoINTIWeb
    @GrupoINTIWeb 5 ปีที่แล้ว

    Yeah ...so far....great video Man. I am suscribed.

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

    Great work thank you

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

    Also we can put the belongs to user relation in that Trait

  • @PR0D0T4
    @PR0D0T4 5 ปีที่แล้ว

    Very good video! Thanks and keep going!

  • @blpraveen1436
    @blpraveen1436 7 หลายเดือนก่อน

    If use Raw query(DB::table) still observerbs and golbal scrop works?

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

    Can we implement the same idea with multi databases from single code base?
    Thank you for the great tutorial

  • @igoreksazonov
    @igoreksazonov 5 ปีที่แล้ว +1

    Thanks for the video! Where to learn deeper about Observers?

    • @LaravelDaily
      @LaravelDaily  5 ปีที่แล้ว

      Here it is in the official docs: laravel.com/docs/5.7/eloquent#observers

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

    ty man, so helpful

  • @gabrielcrespo9338
    @gabrielcrespo9338 5 ปีที่แล้ว

    Thanks bro.

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

    Hi, i want to ask.
    i want show data where created_by_user_id id null?
    if possible?

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

    I also found one error on frontend for another users not seen any data on frontend also what solution for that.

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

    Is it Multi-Tenancy can be achieved with Policy?

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

    COOL AF

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

    Great example. The only thing I dont get is how the bootMultitenantable() function is called. Is it because Larabel executes everyrhing that starts with bootXxxxc?

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

    what about super admin because of tenant super admin also not seen others records then how give all access to super admin

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

    Hi, my requirement is I want a user to register to multiple hotels with same email id based on different url for hotels like localhost/radisson_london with id =1 and localhost/radisson_stockholm with id =2. and based on the url i want to filter all models with hotel_id.
    Or is there any stuff on saas with multi tenancy?

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

    Waow!!

  • @RahulKumar-rk1tf
    @RahulKumar-rk1tf 5 ปีที่แล้ว +1

    Thank you so much, useful stuff I am going to implement in my current project, Could you please tell me the chrome add-ons, which you are using for form filllup

    • @LaravelDaily
      @LaravelDaily  5 ปีที่แล้ว +1

      It's called Form Filler: chrome.google.com/webstore/detail/form-filler/bnjjngeaknajbdcgpfkgnonkmififhfo?hl=en

  • @s.camilogomez2799
    @s.camilogomez2799 5 ปีที่แล้ว +1

    Hello, excellent explanation. Can I implement the Multi-Tenancy part in my project?

    • @PovilasKorop
      @PovilasKorop 5 ปีที่แล้ว +2

      If you're a developer, of course you can :)

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

    Hello! Since the creating method sets the user_id automatically I cant create entities related to different users in my tests. Is there a way to solve this?

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

    U are just amazing is there any package out there for this ?

  • @s.camilogomez2799
    @s.camilogomez2799 5 ปีที่แล้ว

    hello, a query: how do I use the Formrequest class and validate a single field in the whole system; example: a person's identification number must be unique throughout the system. But it does not work because the model only validates return $ builder-> where ('user_id', auth () -> id ()) ... lets me repeat the records. It only validates the unique field if it has been created by the user who is logged in. I would like to know how to make that exception. Thank you

  •  5 ปีที่แล้ว +1

    If you want show all to admin user and keep this logic (multi-tenancy)? How you resolve this?

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

      You should add a condition to check whether the authenticated user's role is admin or not, if yes, then you should not filter by its user id, else, do exactly like the video

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

    good tutorial sir
    thanks for same
    by the way not working on
    \DB::table('xyz')->whereRaw('a

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

    Anybody could help me how to pass this trait to allow another trait for checking the whole rows.
    I'm using cviebrock/sluggable to create unique slug, but it cannot check the whole table and produce error when different users insert rows that generate identic slug.

  • @BRO-sp8ld
    @BRO-sp8ld 2 ปีที่แล้ว

    Is it better to create model observer for fill only created_by_user_id filed instead put this logic into controller create method like this: Hotel::create($request->validated + ['created_by_user_id' => Auth::id()]), and why if it is?

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

      It's your personal preference

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

    can we use like this if user has many hotel and hotel has many user like many to many table
    user
    hotel
    hotel_user

  • @ergurkha3157
    @ergurkha3157 5 ปีที่แล้ว +2

    Multi-tenant with different database conecction?

    • @LaravelDaily
      @LaravelDaily  5 ปีที่แล้ว +1

      As I said in the end of this video, this is a much broader topic, requires a full course on it. In short, I can recommend Laravel Tenancy package: laravel-tenancy.com/

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

    how to implement this with api resource?

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

    That is awesome .. how about multi domain each hotel has one domain

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

      Here's a demo: th-cam.com/video/D25CxsZONvc/w-d-xo.html

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

    Nice video thanks a lot, I have a question, if an admin needs to check the info of the users in some admin section when he ask laravel for the multitenantable objects that each user created, wich is the correct way to get the info? I mean the admin wont see it because he didn't created it

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

      For admin, you can use some if-statement to check if he's an admin and then remove the global scope, like User::withoutGlobalScope()->get() if I remember syntax correctly.

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

    Hi Povilas, Its a great tutorial you've put together there. If I could ask your advice please? If I were the have the following scenario how would you go about expanding the bootmultitennantable function to accommodate?
    if there was Admin, Agent and User. If the user created their own bookings and therefore each booking belongs to a hotel the user could therefore see all hotels in order to make the booking. Then the agent has access only to the bookings that are in the hotels under his creation. Admin would have access to all hotels and bookings.
    Thankyou in advance.
    Best regards
    Stephen

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

      Hi Stephen. Unfortunately this is not an answer I can give in a short TH-cam comment, would have to set up a real project and play around with variables. If you want me to do it, you can book some of my time, email me povilas@laraveldaily.com

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

      @@PovilasKorop No problem, Thanks for the prompt reply :)
      I think I've worked something out however may get in touch soon as getting a little beyond my capabilities.
      Best regards
      Stephen

  • @easyvideott7505
    @easyvideott7505 5 ปีที่แล้ว

    The data is actually stored in only one Database right? Is there some way or a package to have a separate Database for each agent?

    • @PovilasKorop
      @PovilasKorop 5 ปีที่แล้ว

      Yes it's in one database. For multiple databases it's 5x more difficult solution, but I can recommend a package for it - laravel-tenancy.com/

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

      @@PovilasKorop but not working

  • @s.camilogomez2799
    @s.camilogomez2799 5 ปีที่แล้ว

    Hello, could you please help me with a question, please: when I am registering a new booking and, I inspect an item and manually enter an ID of another hotel that has been created by another user, it is being stored. How could I control that, that is, do not let save a hotel ID that has not been created by the user who is logged in. Thank you

    • @PovilasKorop
      @PovilasKorop 5 ปีที่แล้ว

      Sorry it's impossible to help you without actually seeing the code and the structure, so it's not a help that can be provided in TH-cam comment. Email me povilas@laraveldaily.com with more details and maybe I could help.

  • @igoreksazonov
    @igoreksazonov 5 ปีที่แล้ว +1

    How do you feel about Nova?

    • @LaravelDaily
      @LaravelDaily  5 ปีที่แล้ว +1

      I was asked that question last year at Laracon EU when Nova was released. Since we have our QuickAdminPanel which is direct competitor, I'm biased to answer. Also, I haven't used Nova myself in production projects so my knowledge is mostly from reviews and opinions I've read online. And there are two types of people:
      1. Nova is awesome, Taylor did a brilliant job!
      2. Nova looks awesome! But wait, how do you customize X? Oh, you need to create your own components for that? Oh, and there's no good documentation with examples about it? And no Stackoverflow topics? Hmmm...
      In other words, Nova seems to be perfect for smaller panels, but heard negative reviews from people who tried to do something custom with it.

    • @igoreksazonov
      @igoreksazonov 5 ปีที่แล้ว

      @@LaravelDaily Im agree! Nova now is not so stable, but for the moment. I think we have to wait some time, like a year

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

      @@igoreksazonov Say whaaa? Nova was stable from the minute it came out. There is no need for documentation, the code itself tell you how to do things.

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

    Source code through GitHub not working having many errors!!! And there is no vendor folder???

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

      Vendor folder appears only when you run composer install. And what exactly is not working for you, give more details and I can comment.

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

      @@PovilasKorop composer installed already!!. Is we have to installed it for every time?

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

      @@nabeelahmad9896 If you have run "composer install", then there should be a vendor folder generated. For more details, please read how Composer works, I'm not teaching that basic knowledge in this video.

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

    My "auth()->id()" is always returning null. What should i do in this case?

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

      It means you're not logged in.

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

      The login works fine, i can access the user on other pages. Only in the trait returns null.

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

      @@matheusoliveira9516 very weird, so on that page where you use that trait, that page is probably missing the login session? Sorry I can't debug it for you

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

      @@PovilasKorop Yeah, I think it's the session. Thanks man.

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

      @@matheusoliveira9516 How did you solve this issue?

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

    Can you tell me what this admin template name is?

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

      Template is AdminLTE. Generator is our own quickadminpanel.com

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

    FYI, this is multitenancy in a single database..