Reputation: 1127
I'm trying to create something like a blogging system inside the Laravel Nova.
I have a table named articles
as well as abstract model AbstractArctile
.
I also have 3 categories:
App\Models\News\Article
extending the App\Models\Abstract\AbstractArticle
App\Models\Digests\Article
extending the App\Models\Abstract\AbstractArticle
App\Models\Offtopic\Article
extending the App\Models\Abstract\AbstractArticle
The table articles
has a field named category, and there are 3 types of categories: news, digests, offtopic.
Besides the extending the abstract model, each resource model also has one attribute defined, which is it's category in the following manner:
/**
* To which category this article belongs to
* @var array
*/
protected $attributes = [
'category' => 'news'
];
I have no problem creating the articles under the specified categories in the Nova, however, instead of showing the articles from specified category, it displays articles from all categories on all resources.
Is there a way to display articles only from certain category on a given resource?
One abstract model -> 3 resources extending that model (with category attribute defined) -> how to display only items from that category inside the nova resource?
Upvotes: 2
Views: 2676
Reputation: 394
In Nova, you can use indexQuery,detailQuery and editQuery as well and customize your resource results.
https://nova.laravel.com/docs/4.0/resources/authorization.html#index-filtering
Upvotes: 0
Reputation: 8850
You can make use of Laravel eloquent query scope.
Add global scope like below to all 3 models (App\Models\News\Article
, App\Models\Digests\Article
, App\Models\Offtopic\Article
), which is a easy way to make sure every query for a given model receives category constrains.
protected static function boot()
{
parent::boot();
static::addGlobalScope('category', function (Builder $builder) {
$builder->where('category', 'news'); // Change the value depends on the model
});
}
Hope this will help you.
Upvotes: 8