<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Eduardo Geschonke on Medium]]></title>
        <description><![CDATA[Stories by Eduardo Geschonke on Medium]]></description>
        <link>https://medium.com/@edu_96787?source=rss-d5f8faf7fd3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*DNkNRqRmTM9JL4g4.</url>
            <title>Stories by Eduardo Geschonke on Medium</title>
            <link>https://medium.com/@edu_96787?source=rss-d5f8faf7fd3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 10:02:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@edu_96787/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Multi-Tenancy Laravel App With Multi-Database using hyn/multi-tenant]]></title>
            <link>https://medium.com/@edu_96787/building-a-multi-tenancy-laravel-app-with-multi-database-using-hyn-multi-tenant-4f1cdbf99de?source=rss-d5f8faf7fd3------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f1cdbf99de</guid>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Eduardo Geschonke]]></dc:creator>
            <pubDate>Thu, 24 May 2018 17:05:24 GMT</pubDate>
            <atom:updated>2018-05-24T21:17:30.072Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi, In this tutorial I will show how i was able to create an app with multiple databases and visualization of data of each client in an administrative area.</p><p>First, some important information:</p><ul><li>My English in writing mode is not the best (I’m Brazilian);</li><li>Let’s use the package <a href="https://github.com/hyn/multi-tenant">https://github.com/hyn/multi-tenant</a></li><li>Laravel Version: <strong>5.6.23</strong></li><li>hyn/multi-tenant version: <strong>^5.1</strong></li><li>The database is MariaDB.</li></ul><h4>Application setup and package installation</h4><p>Before we get started, let’s clear some things about the <strong>hyn/multi-tenant</strong> package:</p><p><a href="https://laravel-tenancy.com/docs/hyn/5.x/concepts">Tenancy</a></p><p>Directly from the documentation:</p><h3>Website</h3><p>You can think of a website as a single mirror of the code base, re-using your default app code. In addition it is also possible to have very <a href="https://laravel-tenancy.com/docs/hyn/5.x/structure">tenant specific logic</a>, for instance routes, a vendor folder, media and language files.</p><h3>Hostname</h3><p>A hostname is a <a href="https://www.godaddy.com/garage/industry/tech-svcs/it/whats-a-fully-qualified-domain-name-fqdn-and-whats-it-good-for/">Fully Qualified Domain Name</a> (for instance sub.example.com). Your application handles incoming requests to specific hostnames. Tenancy inspects these requests and <a href="https://laravel-tenancy.com/docs/hyn/5.x/identification">sets up the tenancy environment</a> according to a matching hostname or default fallback.</p><h3>Customer</h3><p>A customer is the binding entity between websites and hostnames. They allow freely combining the two. A website and hostname can have a customer, they don’t need to be the same by the way.</p><p>In broader terms: A Customer can have many hostnames (subdomains), can also have many websites (each website consists of a database) and a hostname is linked to a website, as a website is linked to a hostname</p><p><strong>Let’s start!?</strong></p><p>Access the terminal and install your laravel application as usual:</p><pre>laravel new multi-tenancy</pre><p>If you are using <strong>Laravel Valet</strong>, an <strong>valet open </strong>in the terminal will open the project for you :)</p><p>Before installing the multi-tenant package, we need to make some adjustments to the initial setup of laravel.</p><p>Inside <strong>config/database.php</strong> we will change the default connection of <strong>mysql</strong> to <strong>system</strong> if not, when installing the package, the application will throw informing that such a database was not properly configured.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*sd-JTQ9R06i44dDJMB77Fw.png" /><figcaption>Change the default connection to use system connection. This is required by the hyn/multi-tenant package</figcaption></figure><p>After changing the configuration, let’s add/change some of the variables inside the <strong>.env</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95dd68153df525daeb31227fd583179e/href">https://medium.com/media/95dd68153df525daeb31227fd583179e/href</a></iframe><p>Just for convenience I named the database as <strong>system_tenancy</strong> but you can give the name it fits to you. The variables of lines <strong>8, 9 and 10</strong> will be explained later.</p><h4>Installing and configuring hyn/multi-tenant</h4><p>And we’re going to the terminal again. Run the command:</p><pre>composer require hyn/multi-tenant</pre><p>If all of the above settings are correct, installation has occurred normally without any warnings.</p><p><strong>Now the part that really matters:</strong></p><pre>php artisan vendor:publish --tag=tenancy</pre><p>Access the file in <strong>config/tenancy.php</strong> and we will make some modifications.</p><p>All items are well described, but we are only interested in these here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/979/1*ojxs0f56v9LeXqo3VwEXFA.png" /></figure><p>Disabling <strong>abort-without-identified-hostname</strong> we will be able to access the application without the need for a subdomain (in the main domain that would be the administrative dashboard).</p><p>Here we will also find the variables listed in our<strong> .env</strong>. Basically they are to auto remove a user and their database whenever we remove a client from the application.</p><p>Below we will change the migrations path of each <strong>customer/tenant</strong>.<br>It is suggested <strong>migrations/tenant</strong> but I believe that <strong>migrations/customers</strong> is more intuitive since the whole application works based on <strong>customers /websites/hostnames</strong> (explained at the beginning of the tutorial).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xfQNeNN_3dwRek5IbTl0VQ.png" /></figure><p>With everything properly adjusted, let’s turn the command</p><pre>php artisan tenancy:install</pre><p>The command will migrate all existing tables in <strong>database/migrations</strong> directly into the <strong>system_tenancy</strong> database. In addtion with these tables the package will add another 03 <strong>(customers, Hostnames and Websites)</strong>.</p><p>If everything went well, then now we can configure our customers o/</p><h4>Creating and configuring our customers</h4><p>Customers are the soul of the application. Also known as tenants (in the concept of multi-tenancy), each customer must have its <strong>database</strong>, with its <strong>tables</strong> and one <strong>customer</strong> must be isolated from another. Luckily, <strong>hyn/multi-tenant </strong>has already taken care of this for us. We just need to create a <strong>seeder</strong> and leave some sample <strong>customers</strong> properly configured.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b774b95d2b47aef53565aadbd5f1d144/href">https://medium.com/media/b774b95d2b47aef53565aadbd5f1d144/href</a></iframe><p>This <strong>seeder</strong> above is responsible for doing all the configuration of the databases. As you can see, we are only using the <strong>hyn/multi-tenant</strong> repositories.</p><p><strong>NOTE</strong>: I am using the <strong>.job</strong> TLD, most likely you are using the <strong>.test</strong>, so do not forget to change it in the <strong>seeder</strong>.</p><p>After that, go to the terminal and run the command:</p><pre>php artisan db:seed --class=&quot;BuildDatabasesForTenants&quot;</pre><p>If no error occurs (hopefully), you will see that there are 3 databases created in addition to 3 customers, 3 hostnames and 3 websites in the main database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*_N5Bu0rUH458znHdpdrnFg.png" /><figcaption>websites list</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*pTjPWjNI2brvhxv41evWOg.png" /><figcaption>customers list</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*fPRm2WnVB1evlJUjdllbfg.png" /><figcaption>hostnames list</figcaption></figure><p>READY. You can now access the following addresses:</p><ul><li><a href="http://foo.multi-tenancy.job/">http://foo.multi-tenancy.job</a></li><li><a href="http://foo.multi-tenancy.job/">http://bar.multi-tenancy.job</a></li><li><a href="http://foo.multi-tenancy.job/">http://baz.multi-tenancy.job</a></li></ul><p>The welcome screen of laravel will appear for both projects, but make no mistake, in every link you are connecting to different database.</p><h4>Working with customers</h4><p>With everything in place, let’s create some migrations for customers and test if everything is working accordingly.</p><p>Let’s create some folders within the app. You can ignore the files, just create the folders. Oh, I hide the other laravel folders just for easy viewing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*TDtL6EoKQw9Pmah0kLRz6w.png" /></figure><p>Created the folder structure, let’s create migrations and customers’ factories.</p><h4>Lots of Commands</h4><pre>php artisan make:migration customer_create_posts_table --path=&quot;database/migrations/customers&quot;</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4305647afb7cd67d7def2554b17a1c0/href">https://medium.com/media/f4305647afb7cd67d7def2554b17a1c0/href</a></iframe><p>Simple migration, nothing unusual. It was just created inside the customers folder.</p><pre>php artisan make:model Customer/Models/Post</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25cc206027ecbbc528e9f39e88520a83/href">https://medium.com/media/25cc206027ecbbc528e9f39e88520a83/href</a></iframe><p>The only difference from this model to the Laravel pattern is that we are using the <strong>hyn</strong> trait that identifies this model as belonging to a customer connection. Of course, we are relating to the user who is also in the same namespace (exemplified namespace as complete for visualization only).</p><pre>php artisan make:factory PostFactory</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ee48dab8a90e2ec09f09530af3608ac/href">https://medium.com/media/1ee48dab8a90e2ec09f09530af3608ac/href</a></iframe><p>The factory is in the default folder <strong>database/factories</strong> the only difference is that we are using the customer model.</p><pre>php artisan make:migration customer_create_users_table --path=&quot;database/migrations/customers&quot;</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49e327f1be2c65b85bc128c8b2ed1b38/href">https://medium.com/media/49e327f1be2c65b85bc128c8b2ed1b38/href</a></iframe><pre>Change the default user factory that is already created.</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42f5e0f40b5aa2b2a5bde3d9c0e9bfd6/href">https://medium.com/media/42f5e0f40b5aa2b2a5bde3d9c0e9bfd6/href</a></iframe><pre>php artisan make:model Customer/Models/User</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/702a14ed31eafa57c6c3cbf4b6feeb13/href">https://medium.com/media/702a14ed31eafa57c6c3cbf4b6feeb13/href</a></iframe><p>Here comes the cool part, this model extends App\Shared\Models\User as Shared but uses the <strong>tenant trait </strong>as a form of connection. More details of the model below:</p><pre>php artisan make:model Shared/Models/User</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/245546a025c6d2d04e56a4fb224ff2ef/href">https://medium.com/media/245546a025c6d2d04e56a4fb224ff2ef/href</a></iframe><p>Yes, I know what you’re thinking, “There’s nothing different than the namespace.” That’s right, models within <strong>App\Shared\Models</strong> have methods, properties and other codes that both (system and customer) will use, however be careful of each model to set the connection (trait) that will use.</p><pre>php artisan make:model System/Models/User</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e84e4c11a777270039d10f8f1cac6417/href">https://medium.com/media/e84e4c11a777270039d10f8f1cac6417/href</a></iframe><p>At this point you should know that <strong>UsesSystemConnection</strong> means that this model will connect to the main database. Oh, any customer can has relationships to models of the system because it is the main database.</p><p>Okay, now let’s create the customer seeder and feed the databases.</p><pre>php artisan make:seeder TenancyDatabaseSeeder</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e6af991460f4f02071e87f43836cd5b/href">https://medium.com/media/9e6af991460f4f02071e87f43836cd5b/href</a></iframe><p>Simple seeder, just creating some users and adding 3 posts to each of them.</p><h4>Testing the application and viewing the results</h4><p>Let’s run the customer seeds and see the databases with all records.</p><pre>php artisan tenancy:db:seed --class=TenancyDatabaseSeeder</pre><p>Now let’s visualize the results in the browser.</p><p>Create some routes to view the customers and also view the users of each customer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4736e7693944b02f877df0e9c1c0d102/href">https://medium.com/media/4736e7693944b02f877df0e9c1c0d102/href</a></iframe><p>Go to<strong> http://multi-tenancy.job/customers</strong> <strong>(in my case it is .job)</strong> and view all the customers that have been created in the system_tenancy database.</p><p>Go to<strong> http://foo.multi-tenancy.job/users</strong> and view the users, with their related posts</p><p>Go to<strong> </strong><a href="http://foo.multi-tenancy.job/users"><strong>http://bar.multi-tenancy.job/users</strong></a> and view the users, with their related posts</p><p>Go to<strong> </strong><a href="http://foo.multi-tenancy.job/users"><strong>http://baz.multi-tenancy.job/users</strong></a> and view the users, with their related posts</p><p>We can say that everything is ready, and from here you can already work. Of course, You have to read the package documentation for more details (<a href="https://laravel-tenancy.com/docs/hyn/5.x/index">https://laravel-tenancy.com/docs/hyn/5.x/index</a>).</p><h4>BONUS: How to access customer data from the admin dashboard.</h4><p>Super simple, let’s create an “api” and return the user data using libraries like guzzlehttp/guzzle or kitetail/zttp.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9862c5571a6ac9284beac3d2949e4103/href">https://medium.com/media/9862c5571a6ac9284beac3d2949e4103/href</a></iframe><p>I believe this was a good introduction to how to work with multi-tenants.</p><p>In a next tutorial I will teach you how to work with authentication and permissions management on each tenant.</p><p>A big hug.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f1cdbf99de" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My First attempt of use this!]]></title>
            <link>https://medium.com/@edu_96787/my-first-attempt-of-use-this-8b8fe46ac3ff?source=rss-d5f8faf7fd3------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b8fe46ac3ff</guid>
            <dc:creator><![CDATA[Eduardo Geschonke]]></dc:creator>
            <pubDate>Thu, 24 May 2018 01:35:08 GMT</pubDate>
            <atom:updated>2018-05-24T01:35:08.514Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a history of a guy that tries use Medium!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b8fe46ac3ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>