Resource returns wrong entry in case of multiple models in same table #4117
-
Description:Nova resource is returning first matching result from database table even if the model is using global scope. Because of legacy integration I have multiple models assigned to one table which is presenting different categorization for models. I have models such as
To properly handle this I have created global scope for each model to filter right entities. App/Models/ArticleCategory.php class ArticleCategory extends Model
{
public $incrementing = false;
protected $connection = 'sqlsrv';
protected $table = 'usergroup';
protected $primaryKey = 'groupname';
protected $keyType = 'string';
protected static function booted()
{
static::addGlobalScope('articleCategory', function (Builder $builder) {
$builder->where('typename', 'art1');
});
}
}App/Models/SupplierCategory.php class SupplierCategory extends Model
{
public $incrementing = false;
protected $connection = 'sqlsrv';
protected $table = 'usergroup';
protected $primaryKey = 'groupname';
protected $keyType = 'string';
protected static function booted()
{
static::addGlobalScope('supplierCategory', function (Builder $builder) {
$builder->where('typename', 'supp1');
});
}
}App/Nova/SupplierCategory.php class SupplierCategory extends Resource
{
public static $model = \App\Models\SupplierCategory::class;
public function title()
{
return $this->groupname.' '.$this->groupdesc;
}
public function fields(NovaRequest $request)
{
return [
ID::make(null, 'groupname')->sortable(),
Text::make('Name', 'groupdesc')->sortable(),
];
}
}Now, with this setup when I visit resource http://127.0.0.1:8000/nova/resources/supplier-categories/10, Nova shows the first matching resource from the database without global scope (image below). The resource list at http://127.0.0.1:8000/nova/resources/supplier-categories is showing everything correctly, and Tinker also returns correct entity when running Detailed steps to reproduce the issue on a fresh Nova installation:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Nova disables global scope by default in detail query in order to allow the user to access trashed models, however you can re-enable it by adding the following to each resource or /**
* Build a "detail" query for the given resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function detailQuery(NovaRequest $request, $query)
{
static::newModel()->registerGlobalScopes($query);
return parent::detailQuery($request, $query);
} |
Beta Was this translation helpful? Give feedback.

Nova disables global scope by default in detail query in order to allow the user to access trashed models, however you can re-enable it by adding the following to each resource or
App\Nova\Resource: