PHP code example of hipsterjazzbo / landlord

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

    

hipsterjazzbo / landlord example snippets


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

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

$app->register(HipsterJazzbo\Landlord\LandlordServiceProvider::class);

Landlord::addTenant('tenant_id', 1);

$tenant = Tenant::find(1);

Landlord::addTenant($tenant);

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 HipsterJazzbo\Landlord\BelongsToTenants;

class ExampleModel extends Model
{
    use BelongsToTenants;
}


use Illuminate\Database\Eloquent\Model;
use HipsterJazzbo\Landlord\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();
bash
php artisan vendor:publish --provider="HipsterJazzbo\Landlord\LandlordServiceProvider"