A single database multi-tenancy package for Laravel & Lumen 5.2+.
ATENTION
This repository has moved to https://github.com/nunomazer/laravel-samehouse
This project forked from hipsterjazzbo/landlord
A simple, single database multi-tenancy solution for Laravel 5.2+
License: MIT License
A single database multi-tenancy package for Laravel & Lumen 5.2+.
ATENTION
This repository has moved to https://github.com/nunomazer/laravel-samehouse
Please move your installation to https://github.com/nunomazer/laravel-samehouse
I'm in the middle of upgrading PHP to 7.3.9, Laravel to 5.6, and my app so I am migrating to your fork (which thank you by the way). But part of my code is trying to do a query without the tenants and it's failing.
// in DataProvider class
protected function getPhotoQuery()
{
return Photo::allTenants() // <---- error here
->select('photos.*')
->join('photo_player', 'photos.id', '=', 'photo_player.photo_id')
->where('photo_player.season_id', '=', $this->playerSeason->season_id)
->where('photo_player.player_id', '=', $this->player->id)
->orderBy('photos.created_at', 'desc');
}
Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "Call to a member function newQueryWithoutTenants() on null"
Stacktrace:
#1 Symfony\Component\Debug\Exception\FatalThrowableError in PATH\vendor\torzer\awesome-landlord\src\BelongsToTenants.php:69
Here's the entirety of my Photo class
<?php
namespace App\Models;
use Torzer\Awesome\Landlord\BelongsToTenants AS BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
use BelongsToTenant;
public function getPhotoAttribute()
{
return config('urls.photos') . '/' . $this->file . '.jpg';
}
public function getThumbAttribute()
{
return config('urls.photos') . '/thumbs/' . $this->file . '.jpg';
}
public function getJSONData(Player $player = null)
{
$json = ['main'=>null, 'also'=>[]];
$playersTemp = $this->players;
if($player != null){
$playersTemp = $playersTemp->reject(function($p) use($player) {
return $p->id === $player->id;
});
$json['main'] = $player->toArray();
}
if($playersTemp->count()){
$json['also'] = $playersTemp->toArray();
}
return json_encode($json);
}
public function albums()
{
return $this->belongsToMany('App\Models\PhotoAlbum', 'album_photo');
}
public function players()
{
return $this->belongsToMany('App\Models\Player');
}
}
Hey, more of a question than an issue. I see you have created compatibility with L6 and this appears to be actively. What are the key differences between your package vs. the original?
I have a mobel that uses tenancy and a model observer, but the second I try to add the observer the query stops adding the tenant conditions. I even have it to where the observer class has no actual code to run, so it appears to be just the act of adding it breaks.
// app.php#providers
// lots of others not shown, but order persists
Torzer\Awesome\Landlord\LandlordServiceProvider::class,
App\Providers\AppServiceProvider::class,
App\Providers\TenantServiceProvider::class,
// AppServiceProvider#boot
Game::Observe(PersistToObserver::class);
// TenantServiceProvider#boot
// lots of other things that are confirmed working properly
Landlord::addTenant('site_id', $site->id);
Landlord::addTenant('season_id', $season->id);
// PersistToObserver
class PersistToObserver {
// everything is commented out
}
// Game Model
class Game extends Model implements Shareable, IPersistTo
{
use BelongsToTenants, Event, UsesCustomCollection;
// other model stuff
}
// Route controller query in question
$results = Game::with('location')
->withCount(['album', 'stats', 'updates'])
->results()
->take(4)
->get();
I've stripped that query down to nothing but Game::get() and it still doesn't apply. All of those withCount models have the same tenancy and the sub query for those work all the time. Its only when that Game is observed. Comment out that line in AppService#boot and tenancy works again.
On Laravel 5.6.39 and 3.1.0 of this pacakge
Laravel 6.0 is released
See PR #7 😄
Hi,
I have followed your instructions but this package isnt injecting the where "company_id = X" into the SQL queries.
When all call $usersList = User::all('name', 'email','phone_number','profile_url'); in the userController, the company_id is not injected in the sql. I even had \Landlord::enable() called before that to make sure it is on.
What do you think I could be doing wrong?
Thanks
Add support for new version Laravel 5.8
public function handle($request, Closure $next)
{
if (auth()->check()) {
if (auth()->user()->tenant_id) {
Landlord::addTenant('tenant_id', \auth()->user()->tenant_id);
}
}
dd(Landlord::getTenants());
return $next($request);
}
dump:
Illuminate\Support\Collection {#1315 ▼
#items: array:1 [▼
"tenant_id" => 2
]
}
public static function bootBelongsToTenants()
{
// Grab our singleton from the container
static::$landlord = app(TenantManager::class);
// Add a global scope for each tenant this model should be scoped by.
static::$landlord->applyTenantScopes(new static());
dd(static::$landlord);
// Add tenantColumns automatically when creating models
static::creating(function (Model $model) {
static::$landlord->newModel($model);
});
}
dump:
Torzer\Awesome\Landlord\TenantManager {#1314 ▼
#enabled: true
#tenants: Illuminate\Support\Collection {#1315 ▼
#items: []
}
#deferredModels: Illuminate\Support\Collection {#1316 ▼
#items: array:1 [▼
0 => Modules\System\Entities\Usergroup {#1317 ▶}
]
}
}
But when I put the content of middleware on applyTenantScopes function it works and my request is filtered by tenant_id perfectly:
public function applyTenantScopes(Model $model)
{
if (!$this->enabled) {
return;
}
if (auth()->check()) {
if (auth()->user()->tenant_id) {
$this->addTenant(auth()->user()->tenant, \auth()->user()->tenant_id);
}
}
....
I wanna understand what I'm doing wrong, please
Hi! Let say i have users who are linked to a shop. Users can make shops and other data like customers and etc are linked via the shop model.
Do I need to add multiple tenants?
LandLord::addTenant($request->user());
users should still be able to create shops but can only view created shops
LandLord::addTenant($request->shop());
I have a model that extends:
Bavix\LaravelClickHouse\Database\Eloquent\Model;
Would it be possible to allow Landlord to work with other Classes that implement the same interface?
Hey All -
Do you have to run the addTenant()
function for every request you make? Is there a best practice for running this on all request for a model?
Thanks,
Mike
Referencing the conversation in hipsterjazzbo#66, you will note that the end of the chain terminates with renanwilliam having introduced a critical bug and making the excuse that it was okay because the code he wrote passed the test package. Which was true, I confirmed it myself.
However, renanwilliam ignored the fact that it did not pass the "does this do what the docs says it will do" test.
That is probably the most important test for any library.
I'm using next code
Landlord :: addTenant ('schema_id', 3);
$ac = \App\Accounts::all();
Return
{
id: 1
schema_id: 1,
name: 'Demo 1'
},
{
id: 2,
schema_id: 2,
name: 'Demo 2'
}
what am I doing wrong?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.