How to use Laravel Package LandLord

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
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

    public function handle($request, Closure $next)
    {        
        if (Auth::check()) {
            $tenantId = Auth::user()->company_id;

            Landlord::addTenant('company_id', $tenantId); // Different column name, but same concept
        }

        return $next($request);
    }
}

My routes /web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();
Route::group(['middleware' => ['multitenant']], function () {
    Route::get('/home', 'HomeController@index');

    //Clients
    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

+4
source share
1 answer

, middleware. .

middleware routes/web.php:

Route::group(['middleware' => ['landlord']], function () {
    // Your routes
});

landlord middleware :

public function handle($request, Closure $next)
{        
    if (Auth::check()) {
        $tenantId = Auth::user()->company_id;

        Landlord::addTenant('company_id', $tenantId); // Different column name, but same concept
    }

    return $next($request);
}

trait , :

use HipsterJazzbo\Landlord\BelongsToTenant;

class User extends Authenticatable
{
    use BelongsToTenant;
}

, , config/app.php landlord providers:

'providers' => [
    // ...
    HipsterJazzbo\Landlord\LandlordServiceProvider::class
    // ...
],

aliases:

'aliases' => [

    // ...
    'Landlord'   => HipsterJazzbo\Landlord\Facades\Landlord::class,
    // ...
],

, , composer dump-autoload , .

+3

Source: https://habr.com/ru/post/1662345/


All Articles