Skip global scope on open modal and try to fetch record data.

Hello, everyone! I'm using Filament PHP in my project. I’ve applied a global scope to one of my models to filter records, and it works as expected in most cases. However, I’ve noticed an issue: if I try to fetch the same record again, the global scope is skipped. After investigating, I found that Livewire seems to fetch the record from ModelSynth. Here’s the relevant part of the code:
function hydrate($data, $meta) {
$class = $meta['class'];

// If no alias is found, this returns `null`
$aliasClass = Relation::getMorphedModel($class);

if (! is_null($aliasClass)) {
$class = $aliasClass;
}

// If no key is provided, an empty model is returned
if (! array_key_exists('key', $meta)) {
return new $class;
}

$key = $meta['key'];

// Fetching the model for restoration
$model = (new $class)->newQueryForRestoration($key)->useWritePdo()->firstOrFail();

return $model;
}
function hydrate($data, $meta) {
$class = $meta['class'];

// If no alias is found, this returns `null`
$aliasClass = Relation::getMorphedModel($class);

if (! is_null($aliasClass)) {
$class = $aliasClass;
}

// If no key is provided, an empty model is returned
if (! array_key_exists('key', $meta)) {
return new $class;
}

$key = $meta['key'];

// Fetching the model for restoration
$model = (new $class)->newQueryForRestoration($key)->useWritePdo()->firstOrFail();

return $model;
}
The issue seems to be with the newQueryForRestoration method, which fetches the record without applying global scopes:
public function newQueryForRestoration($ids)
{
return $this->newQueryWithoutScopes()->whereKey($ids);
}
public function newQueryForRestoration($ids)
{
return $this->newQueryWithoutScopes()->whereKey($ids);
}
If I modify the hydrate function in ModelSynth.php like this:
$model = (new $class)->newQuery()->whereKey($key)->useWritePdo()->firstOrFail();
$model = (new $class)->newQuery()->whereKey($key)->useWritePdo()->firstOrFail();
...it works perfectly, as it applies the global scope during the query. My Livewire version: 3.5.4 My Questions: 1. Is there a better way to handle this without modifying Livewire’s core? 2. Why does Livewire skip global scopes in newQueryForRestoration? Is this intentional behavior? 3. Can I override this behavior in my Filament project to ensure global scopes are applied? 4. Is this a filament issue or livewire issue. Thanks in advance for your help!
No description
No description
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?