PHP code example of thisisdevelopment / laravel-tenants
1. Go to this page and download the library: Download thisisdevelopment/laravel-tenants 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/ */
thisisdevelopment / laravel-tenants example snippets
use Illuminate\Database\Eloquent\Model;
use ThisIsDevelopment\LaravelTenants\Contracts\Tenant;
use ThisIsDevelopment\LaravelTenants\Traits\ProvidesTenant;
class Customer extends Model implements Tenant
{
use ProvidesTenant;
}
use Illuminate\Support\ServiceProvider;
use ThisIsDevelopment\LaravelTenants\TenantsProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
TenantsProvider::setup(Customer::class);
}
}
use Illuminate\Database\Eloquent\Model;
use ThisIsDevelopment\LaravelTenants\Traits\OnTenantDB;
class Test extends Model
{
use OnTenantDB;
}
use Illuminate\Database\Eloquent\Model;
use ThisIsDevelopment\LaravelTenants\Traits\TenantScoped;
use Illuminate\Database\Eloquent\Builder;
class Settings extends Model
{
use TenantScoped;
public function scopeTenant(Builder $query, Customer $tenant): void
{
// custom filtering to limit this to what the current tenant is allowed to see
}
}
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection;
use ThisIsDevelopment\LaravelTenants\Contracts\TenantAuth;
use ThisIsDevelopment\LaravelTenants\Traits\ProvidesTenantAuth;
class User extends Authenticatable implements TenantAuth
{
use ProvidesTenantAuth;
public function getAllowedTenants(): ?Collection
{
return $this->customers()->get();
}
public function getDefaultTenant()
{
return $this->getAllowedTenants()->first()->id;
}
public function customers(): BelongsToMany
{
return $this->belongsToMany(Customer::class, 'customer_users', 'user_id', 'customer_id');
}
public function scopeTenant(Builder $query, Customer $tenant): void
{
$query->whereIn($this->getKeyName(), $tenant->users()->get()->pluck('id')->all());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.