简体   繁体   English

Laravel Nova - 范围关系字段

[英]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?有没有办法向 BelongsToMany 字段添加范围,这样当您附加相关模型时,您只能从受限选项中执行此操作?

Example:例子:

Post belongsToMany Tags (using Spatie/Laravel-Tags).发布belongsToMany标签(使用Spatie/Laravel-Tags)。 Tags belongsToMany Posts .标签belongsToMany Posts

Tags model has id , name , slug , type and timestamp properties.标签模型具有idnameslugtypetimestamp属性。

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.使用 Post 资源上的默认 BelongsToMany 字段(或第三方包)允许我向 Post 添加标签。

App\\Nova\\Post.php应用\\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.但是,我希望只能向该模型添加类型 = 'Books' 的标签。

If you're using the Spatie Tags package, I'd suggest using their Nova Tags package .如果您使用的是 Spatie Tags 包,我建议您使用他们的Nova Tags 包 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 .这将将该资源的可用标签限制为类型为books 的标签。 See the entry on the Nova Tags package and the base Spatie Tags package请参阅Nova Tags 包和基本Spatie Tags 包上的条目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM