Some resources do not require tenant scope queries ,how to skip

Some resources do not require tenant scope queries ,how to skip ScopeEloquentQueryToTenant is only useful in lists, and there may still be issues when creating and editing them
1 Reply
@uwascan
@uwascan15mo ago
Try this: Create a Marker Interface as show below
namespace App\Contracts;

interface IMultiTenantModel
{

}
namespace App\Contracts;

interface IMultiTenantModel
{

}
create a Base Resource Class that all Your Filament Resource classes will extend as shown below:
namespace App\Resources\Base;

use App\Contracts\IMultiTenantModel;
use Filament\Facades\Filament;
use Filament\Resources\Resource;
use Illuminate\Database\Eloquent\Builder;

abstract class AppBaseResource extends Resource
{
public static function getEloquentQuery(): Builder
{
$query = static::getModel()::query();

if ($tenant = Filament::getTenant()) {
if (app(static::getModel()) instanceof IMultiTenantModel) {
static::scopeEloquentQueryToTenant($query, $tenant);
}
}

return $query;
}
}
namespace App\Resources\Base;

use App\Contracts\IMultiTenantModel;
use Filament\Facades\Filament;
use Filament\Resources\Resource;
use Illuminate\Database\Eloquent\Builder;

abstract class AppBaseResource extends Resource
{
public static function getEloquentQuery(): Builder
{
$query = static::getModel()::query();

if ($tenant = Filament::getTenant()) {
if (app(static::getModel()) instanceof IMultiTenantModel) {
static::scopeEloquentQueryToTenant($query, $tenant);
}
}

return $query;
}
}
Make sure all your Multitenant Models implements the
IMultiTenantModel
IMultiTenantModel
interface. Also make sure all your CreatePages extends from the class below:
namespace App\Resources\Base;

use Filament\Facades\Filament;
use Filament\Resources\Pages\Page;
use App\Contracts\IMultiTenantModel;
use Illuminate\Database\Eloquent\Model;

class AppCreateRecord extends Page
{
/**
* @param array<string, mixed> $data
*/
protected function handleRecordCreation(array $data): Model
{
$record = new ($this->getModel())($data);

if ($tenant = Filament::getTenant()) {
if (app(static::getModel()) instanceof IMultiTenantModel) {
return $this->associateRecordWithTenant($record, $tenant);
}
}

$record->save();

return $record;
}
}
namespace App\Resources\Base;

use Filament\Facades\Filament;
use Filament\Resources\Pages\Page;
use App\Contracts\IMultiTenantModel;
use Illuminate\Database\Eloquent\Model;

class AppCreateRecord extends Page
{
/**
* @param array<string, mixed> $data
*/
protected function handleRecordCreation(array $data): Model
{
$record = new ($this->getModel())($data);

if ($tenant = Filament::getTenant()) {
if (app(static::getModel()) instanceof IMultiTenantModel) {
return $this->associateRecordWithTenant($record, $tenant);
}
}

$record->save();

return $record;
}
}
You can put theses classes any where in your project that makes sense to you. I am in the process of testing this out myself.
Want results from more Discord servers?
Add your server