1. Go to this page and download the library: Download rinvex/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/ */
rinvex / laravel-tenants example snippets
namespace App\Models;
use App\Models\Feature;
use Rinvex\Tenants\Traits\Tenantable;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use Tenantable;
public function features()
{
return $this->hasMany(Feature::class);
}
}
namespace App\Models;
use App\Models\Product;
use Illuminate\Database\Eloquent\Model;
use Rinvex\Tenants\Traits\TenantableChild;
class Feature extends Model
{
use TenantableChild;
public function getRelationshipToTenantable(): string
{
return 'product';
}
public function product()
{
return $this->belongsTo(Product::class);
}
}
// Create a new tenant
app('rinvex.tenants.tenant')->create([
'name' => 'ACME Inc.',
'slug' => 'acme',
'domain' => 'acme.test',
'email' => '[email protected]',
'language_code' => 'en',
'country_code' => 'us',
]);
// Get existing tenant by id
$tenant = app('rinvex.tenants.tenant')->find(1);
// This will only \App\Models\Product::all();
// This will fail with a `ModelNotFoundForTenantException` if it belongs to the wrong tenant
$product = \App\Models\Product::find(2);
// Will antProducts = \App\Models\Product::forAllTenants()->get();
// Will NOT be scoped, and will return results from ALL tenants, just for this query
$allTenantProducts = \App\Models\Product::withoutTenants()->get();
// Get instance of your model
$product = new \App\Models\Product::find(1);
// Get attached tenants collection
$product->tenants;
// Get attached tenants query builder
$product->tenants();
$tenant = app('rinvex.tenants.tenant')->find(1);
// Update title translations
$tenant->setTranslation('name', 'en', 'New English Tenant Title')->save();
// Alternatively you can use default eloquent update
$tenant->update([
'name' => [
'en' => 'New Tenant',
'ar' => 'مستأجر جديد',
],
]);
// Get single tenant translation
$tenant->getTranslation('name', 'en');
// Get all tenant translations
$tenant->getTranslations('name');
// Get tenant title in default locale
$tenant->name;
shell
php artisan rinvex:publish:tenants
shell
php artisan rinvex:migrate:tenants
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.