Laravel Nova - Scope Relationship Fields
Is there a way of adding a scope to the BelongsToMany fields, such that when you attach related models you can only do so from restricted options?
Example:
Post belongsToMany Tags (using Spatie/Laravel-Tags). Tags belongsToMany Posts.
Tags model has id, name, slug, type and timestamp properties.
On the Detail or Update views, I would like to be able to attach Tags to Post.
Using the default BelongsToMany field (or a third-party package) on the Post resource allows for me to add Tags to Post.
App\Nova\Post.php
public function fields(Request $request)
{
BelongsToMany::make('Tags'),
}
However, I would like to only be able to add Tags where type = 'Books' to this model.
Answer
If you're using the Spatie Tags package, I'd suggest using their Nova Tags package. Then you can add the following to your field definitions:
Tags::make('Tags')->type('books'),
This will limit the available tags for that resource to those of type books. See the entry on the Nova Tags package and the base Spatie Tags package
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms