Landlord should work perfectly for this.
Landlord will apply a global scope to Eloquent that filters records automatically. It does this at a level lower than Voyager in the stack, meaning you won't need any extra configuration on the Voyager end of things.
With Landlord you simply add a column to all your CRUD tables that identify ownership of a record, and then you let Landlord know about this.
For example if you go with the column name user_id
, you can then scope to the user anywhere (say in your middleware) with a call as simplistic as Landlord::addTenant('user_id', $userIdHere);
Here's an example middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use App\User;
use Landlord as LandlordManager;
class Landlord {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->user()) {
LandlordManager::addTenant($request->user());
LandlordManager::applyTenantScopesToDeferredModels();
}
}
}
You then go to App/Http/Kernel.php
, find the $routeMiddleware
array and add:
'landlord' => \App\Http\Middleware\Landlord::class
Then go to app/routes/web.php
and apply this middleware to any single route or route group according to your favourite flavour in the docs https://laravel.com/docs/master/middleware. An example being:
Route::group(['prefix' => 'admin', 'middleware'=>'landlord'], function () {
// routes here
});