I am still pretty new to Laravel and have worked on some of the main Larakastas. Now I am starting my first laravel project, but I am fixated on how to use my first "Landlord" package. Basically I need a setup for Multi-Tenants in my application. I have a company table and a user table, the user table has a company_id column. When a company registers it, it successfully creates a company and attaches company_id to the user.
I assume Landlord is the best way to implement an application with multiple tenants, so I worked with the installation instructions and now I have included it in my application.
However, the first line in the USGE section reads: IMPORTANT NOTE: The landlord is stateless. This means that when addTenant () is called, it will only process the current request.
Make sure you add your tenants so that this happens for every request, and before you need models, either in middleware or as part of a stateless authentication method such as OAuth.
And it looks like I need to attach a facade Landlord::addTenant('tenant_id', 1);.
This might be a pretty simple answer I'm looking at, but where is the best place to use it addTenant, and does it need to be updated with each controller or model? Do I have to pin it when a user signs up, uses it on my routes, or uses it as middleware? If this middleware is the next rule to pull the company_id from the current user and use it with addTenant?
Middleware:
public function handle($request, Closure $next){
$tenantId = Auth::user()->tenant_id;
Landlord::addTenant('tenant_id', $tenantId);
return $next($request);
}
UPDATE
Here is my middleware (MultiTenant.php)
<?php
namespace App\Http\Middleware;
use Closure;
use App\User;
use Illuminate\Support\Facades\Auth;
class MultiTenant
{
public function handle($request, Closure $next)
{
if (Auth::check()) {
$tenantId = Auth::user()->company_id;
Landlord::addTenant('company_id', $tenantId);
}
return $next($request);
}
}
My routes /web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::group(['middleware' => ['multitenant']], function () {
Route::get('/home', 'HomeController@index');
Route::resource('clients', 'ClientController');
});
My Client.php Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use HipsterJazzbo\Landlord\BelongsToTenants;
class Client extends Model
{
use BelongsToTenants;
protected $fillable = [
'organization',
];
}
https://github.com/HipsterJazzbo/Landlord#user-content-usage