1. Go to this page and download the library: Download nunomazer/laravel-samehouse library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Http\Middleware;
use App\Models\Company;
use Closure;
use NunoMazer\Samehouse\Facades\Landlord;
class SetTenant
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (auth()->check()) {
if (auth()->user()->company_id) {
Landlord::addTenant('company_id', \auth()->user()->company_id);
}
}
return $next($request);
}
}
Landlord::removeTenant('tenant_id');
// Or you can again pass a Model instance:
$tenant = Tenant::find(1);
Landlord::removeTenant($tenant);
// As you would expect by now, $tenant can be either a string column name or a Model instance
Landlord::hasTenant($tenant);
// $tenants is a Laravel Collection object, in the format 'tenant_id' => 1
$tenants = Landlord::getTenants();
use Illuminate\Database\Eloquent\Model;
use NunoMazer\Samehouse\BelongsToTenants;
class ExampleModel extends Model
{
use BelongsToTenants;
}
use Illuminate\Database\Eloquent\Model;
use NunoMazer\Samehouse\BelongsToTenants;
class ExampleModel extends Model
{
use BelongsToTenants;
public $tenantColumns = ['tenant_id'];
}
// 'tenant_id' will automatically be set by Landlord
$model = ExampleModel::create(['name' => 'whatever']);
// This will only ();
// This will fail with a ModelNotFoundForTenantException if it belongs to the wrong tenant
ExampleModel::find(2);
// Will Model::allTenants()->get()
// Will not scope by 'tenant_id', but will continue to scope by any other tenants that have been set
ExampleModel::withoutGlobalScope('tenant_id')->get();
Landlord::disable();
Landlord::enable();
if (Landlord::isEnabled()) {
// disable to execute admin tasks
Landlord::disable();
...
// enable again
Landlord::enable();
}