Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

archtechx / tenancy: Filter Records and Protect Subdomains

Premium
4:36

In the second part of reviewing tenancy for Laravel for a single database, let's add the tenant_id to the projects table, and I will show you what this package does brilliantly to filter the records.

database/migrations/xxx_add_tenant_id_to_projects_table.php:

Schema::table('projects', function (Blueprint $table) {
$table->foreignId('tenant_id')->constrained();
});

app/Models/Project.php:

class Project extends Model
{
protected $fillable = [
'name',
'tenant_id',
];
}

Filter Records

In the previous lessons of this course, we added global scopes or traits to fill tenant_id automatically and then filtered by that. So, this package does that for us.

In the documentation section of single database tenancy, you may find an example saying to add the BelongsToTenant trait on a primary Model, and all the tenant logic will happen automatically. It will assume this Model has a tenant_id field, which will be filled in automatically.

app/Models/Project.php:

use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
 
class Project extends Model
{
use BelongsToTenant;
 
protected $fillable = [
'name',
'tenant_id',
];
}

But what if we need to get tasks? We don't have the tenant_id field in the tasks table. For such a case, we must...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

MI
Mohammed Ibrahim ✓ Link copied!

Hi, thanks for your amazing tutotial. i have completed everything while i'm using jetstream. but i see that even after comment the bootstraper its still create new databases .. e.g: (tenant1,tenant2..etc). Kindly advise.

PK
Povilas Korop ✓ Link copied!

Sorry I can't debug it for you in the comment here and blindly guess what happened wrong, it requires actual debugging process.

MI
Mohammed Ibrahim ✓ Link copied!

No worries, i found the solution. but plz make videos using jetstream not breeze, thanks.

PK
Povilas Korop ✓ Link copied!

Sorry, but not planning to, for many people Jetstream is an overkill system, Breeze is much more simple to understand.

M
MimisK ✓ Link copied!

archtechx/tenancy in v3 has Stancl\Tenancy\Middleware\ScopeSessions

Route::middleware([
    'web',
    'auth',
    InitializeTenancyByDomainOrSubdomain::class,
    PreventAccessFromCentralDomains::class,	
    \Stancl\Tenancy\Middleware\ScopeSessions::class  
])->group(function () {
M
mcpacific ✓ Link copied!

I've really enjoyed your course. Any chance you could add archtechx / tenancy using path identification instead of subdomain identification? That would be amazing! For branding purposes, it would be nice to have users login from the same path (ie. brandedsomain.test/login), then automatically get redirected to their dashboard using a route prefix like brandeddomain.test/{portal}/dashboard. If they have more than one portal perhaps they could switch it similar to the way the Jetstream Teams are swiched by the user in the panel.

PK
Povilas Korop ✓ Link copied!

Great to hear you enjoyed the course.

For now, we're not planning to expand this course, but we'll see when we get to updating it to Laravel 12, maybe more topics will appear like the one you're suggesting.