PHP code example of globecode / laravel-multitenant

1. Go to this page and download the library: Download globecode/laravel-multitenant 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/ */

    

globecode / laravel-multitenant example snippets


    "globecode/laravel-multitenant": "dev-master"
    

    'GlobeCode\LaravelMultiTenant\LaravelMultiTenantServiceProvider'
    

    /**
     * Get the value to scope the "tenant id" with.
     *
     * @return string
     */
    public function getTenantId()
    {
        return (isset($this->tenant_id)) ? $this->tenant_id : null;
    }
    

    /**
     * Does the current user have an 'admin' role?
     *
     * @return bool
     */
    public function isAdmin()
    {
        // Change to return true using whatever
        // roles/permissions you use in your app.
        return $this->hasRole('admin');
    }
    

     namespace Acme;

    use GlobeCode\LaravelMultiTenant\ScopedByTenant;

    class Example {

        /**
         * Only this line is      /**
         * Query the Tenant the Example belongs to.
         *
         * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
         */
        public function tenant()
        {
            return $this->belongsTo('Acme\Tenant');
        }
    }
    

    

    use GlobeCode\LaravelMultiTenant\ScopedByTenant;

    class AdminExamplesController {

        /**
         * @var Acme\Repositories\ExampleRepository
         */
        protected $exampleRepo;

        public function __construct(ExampleRepository $exampleRepo)
        {
            $this->exampleRepo = $exampleRepo;

            // All queries in this controller on 'exampleRepo'
            // will be 'global' and not scoped.
            $this->exampleRepo->removeTenant();
        }

        /**
         * Display a listing of all Examples.
         *
         * @return Response
         */
        public function index()
        {
            // Global, will *not* be scoped.
            $leads = $this->exampleRepo->getAll();

            $this->view('examples.index', compact('examples'));
        }
    }
    

 namespace Acme\Repositories;

use Illuminate\Database\Eloquent\Model;

use Acme\Example;
use Acme\Repositories\ExampleRepository

class EloquentExampleRepository extends ExampleRepository {

    /**
     * @var Example
     */
    protected $model;

    /**
     * Method extensions from TenantScope class
     */
    protected $whereTenant;
    protected $applyTenant;
    protected $removeTenant;

    public function __construct(Example $model)
    {
        $this->model = $model;

        $this->whereTenant = null;
        $this->applyTenant = null;
        $this->removeTenant = false;
    }

    /**
     * Example get all using scope.
     */
    public function getAll()
    {
        return $this->getQueryBuilder()->get();
    }

    /**
     * Example get by id using scope.
     */
    public function getById($id)
    {
        return $this->getQueryBuilder()->find((int) $id);
    }

    /**
     * Limit scope to specific Tenant
     * Local method on repo, not on TenantScope.
     *
     * @param  integer  $id
     */
    public function whereTenant($id)
    {
        $this->whereTenant = $id;

        return $this;
    }

    /**
     * Remove Tenant scope.
     */
    public function removeTenant()
    {
        $this->removeTenant = true;

        return $this;
    }

    /**
     * Limit scope to specific Tenant(s)
     *
     * @param  int|array $arg
     */
    public function applyTenant($arg)
    {
        $this->applyTenant = $arg;

        return $this;
    }

    /**
     * Expand scope to all Tenants.
     */
    public function allTenants()
    {
        return $this->removeTenant();
    }

    protected function getQualifiedTenantColumn()
    {
        $tenantColumn = Config::get('laravel-multitenant::tenant_column');

        return $this->model->getTable() .'.'. $tenantColumn;
    }

    /**
     * Returns a Builder instance for use in constructing
     * a query, honoring the current filters. Resets the
     * filters, ready for the next query.
     *
     * Example usage:
     * $result = $this->getQueryBuilder()->find($id);
     *
     * @return \Illuminate\Database\Query\Builder
     */
    protected function getQueryBuilder()
    {
        $modelClass = $this->model;

        $builder = with(new $modelClass)->newQuery();

        if ( ! is_null($this->whereTenant))
            $builder->where($this->getQualifiedTenantColumn(), $this->whereTenant);

        if ($this->applyTenant)
            $builder->applyTenant($this->applyTenant);

        if ($this->removeTenant)
            $builder->removeTenant();

        $this->whereTenant = null;
        $this->applyTenant = null;
        $this->removeTenant = null;

        return $builder;
    }
}



use Acme\User;

use GlobeCode\LaravelMultiTenant\TenantScope;

class UsersTableSeeder extends DatabaseSeeder {

    public function run()
    {
        // Manually override tenant scoping.
        TenantScope::setOverride();

        User::create([
            'id' => 1,
            'tenant_id' => null, // an admin
            'email' => '[email protected]',
            'password' => 'secret',

            'created_at' => time(),
            'updated_at' => time()
        ]);

        User::create([
            'id' => 2,
            'tenant_id' => 1000, // a tenant
            'email' => '[email protected]',
            'password' => 'secret',

            'created_at' => time(),
            'updated_at' => time()
        ]);

        ...
    }
}
bash
    php artisan config:publish globecode/laravel-multitenant