- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 Tree
的模型,它应该与 1..n 个 Thing
相关联。 Thing
可以关联到 0..n 个事物。换句话说,这是一个多对多关系,在创建 Tree
时必须选择 Thing
。我的 thing_tree
迁移看起来像这样(还有一个 thing_thing
数据透视表,但这无关紧要):
public function up()
{
Schema::create('thing_tree', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->unsignedBigInteger('tree_id')->nullable();
$table->unsignedBigInteger('thing_id')->nullable();
$table->unique(['tree_id', 'thing_id']);
$table->foreign('tree_id')->references('id')->on('trees')->onDelete('cascade');
$table->foreign('thing_id')->references('id')->on('things')->onDelete('cascade');
});
}
我的树
模型如下所示:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Tree extends Model
{
use HasFactory;
protected $guarded = [];
public function path(){
$path = '/trees/' . $this->id;
return $path;
}
public function associatedThings () {
return $this->belongsToMany(Thing::class);
}
}
Thing
模型如下所示:
public function trees()
{
return $this->belongsToMany(Tree::class);
}
public function parentOf (){
return $this->belongsToMany(Thing::class, 'thing_thing', 'parent_id', 'child_id');
}
public function childOf(){
return $this->belongsToMany(Thing::class, 'thing_thing', 'child_id', 'parent_id');
}
最后,Tree
Nova 资源具有以下字段:
public function fields(Request $request)
{
return [
ID::make(__('ID'), 'id')->sortable(),
Text::make('name'),
ID::make('user_id')->hideWhenUpdating()->hideWhenCreating(),
Boolean::make('public'),
BelongsToMany::make('Things', 'associatedThings')
];
}
没有附加的 Thing
应该不可能创建 Tree
,但创建屏幕如下所示:
我如何在 Nova 中要求这个?
最佳答案
这通过 nova 的默认功能是不可能的。以下是我将如何以最少的努力来解决这个问题(您可能想自己为它创建一个自定义字段)——或者至少我是如何解决过去类似问题的:
<强>1。添加nova checkboxes field到您的项目
<强>2。将该字段添加到您的 nova 资源:
// create an array( id => name) of things
$options = Things::all()->groupBy('id')->map(fn($e) => $e->name)->toArray();
// ...
// add checkboxes to your $fields
Checkboxes::make('Things', 'things_checkboxes')->options($options)
<强>3。添加一个验证器,要求 things_checkboxes
不为空
<强>4。添加一个观察者 php artisan make:observer CheckboxObserver
它将通过复选框同步模型与给定 id 数组的关系,然后从对象中删除复选框字段(因为它将抛出一个未找到的列),所以像这样:
public function saving($tree)
{
// Note: In my case I would use the checkbox_relations method of the HasCheckboxes trait and loop over all checkbox relations to perform the following and get the respective array keys and relation names
$available_ids = array_unique($tree['things_checkboxes']);
// Attach new ones, remove old ones (Relation name in my case comes from the HasCheckboxes Trait)
$tree->things->sync($available_ids);
// Unset Checkboxes as the Key doesn't exist as column in the Table
unset($tree['things_checkboxes']);
return true;
}
<强>5。如果您想继续使用复选框来处理关系,请为观察者中的 retreived
方法反向添加相同的内容。否则,将 ->hideWhenUpdating()
添加到您的复选框字段
我为此添加了一个特征,以便通过复选框轻松地将关系附加到模型:
trait HasCheckboxRelations
{
/**
* Boot the trait
*
* @return void
*/
public static function bootHasCheckboxRelations()
{
static::observe(CheckboxObserver::class);
}
/**
* Defines which relations should be display as checkboxes instead of
* @return CheckboxRelation[]
*/
public static function checkbox_relations()
{
return [];
}
}
checkbox_relations
包含类 CheckboxRelation
的实例数组,它再次包含有关键名、关系名等的信息。
public function __construct(string $relationName, string $relatedClass, string $fieldName, bool $hasOverrides = false, string $relationType = null, array $_fields = [])
此外,我还向默认的 nova 资源添加了一个方法 attachCheckboxRelationFields
,当模型使用该特征时,该方法将在 $fields
上调用。
现在,我只需将 HasCheckboxRelations
添加到模型,添加 checkbox_relations
数组,仅此而已 - 我通过复选框在 nova 资源上建立了 belongsToMany 关系。当然,如果您以这种方式进行管理,您将无法再选择管理数据透视字段——这可能就是为什么 nova 开发人员没有这样做的原因——但对于简单的 belongsToMany 关系,我真的很喜欢使用复选框解决方案默认附加表的。对于具有数据透视字段的数据,您仍然可以使用默认方式。
另请注意,部分代码是即时编写的,因此可能无法开箱即用,但应该传达整体理念。
希望对您有所帮助!
关于php - 如何在创建资源时要求附加相关资源 - Laravel Nova,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66259447/
也许有人已经遇到过类似的问题,可以告诉我。 如何删除导航中的“资源”项。不是一个单独的资源 - 但一般来说,完全删除整个列表,包括标题。 截图:https://prnt.sc/lw1nup 需要:ht
我需要在用户登录后根据其角色重定向到不同的地方。 逻辑很简单。我试着把它放在 redirectPath() Nova 中的函数 LoginController.php ,但我有一个非常奇怪的行为 -
有一个->width() cards 的选项.如何定义高度? 最佳答案 虽然 Nova 不提供 ->height()方法,当你生成一张 Nova 卡片时,它会创建 resources/js/compo
我为 openstack 设置了 2 个节点。 第一个节点包含管理服务,例如 nova-api、nova-scheduler、'glance` ...第二个节点包含网络和计算服务。 当我检查nova-
在 Laravel Nova (v1.0.3) 中,有几种方法可以对资源字段的可见性进行细粒度控制(canSee、showOnDetail 等)。我找不到任何控制字段是否可编辑的方法。如何显示字段,但
在 Laravel Nova 中,我有 BelongsToMany 关系(公司 - 枢纽 - 请求)。 在数据透视表中,我有一些额外的列,我可以使用数据透视字段( https://nova.larav
有没有办法在资源的索引上显示 Laravel\Nova\Fields\Textarea 的前 25 个字符? 最佳答案 只是为了扩展上面的答案,这是我正在使用的函数,它只在切断某些东西时使用省略号:
我有一个自定义资源工具(分类帐输入工具),它可以修改资源的值并将其他行插入相关资源。 “账户”是主要资源。 “AccountTransaction”和“AccountLog”都在创建分类帐条目时被写入
我想使用与 Laravel Nova 在加载组件时使用的加载器相同的加载器。我无法编译它。它永远不会识别从何处加载 LoadingCard 组件。任何帮助,将不胜感激。 T
假设,我想用自定义的(添加一些 CSS 类或添加额外的 HTML)替换 nova/resources/js/components/Form/FieldWrapper.vue,如何做到这一点,而无需更改
使用 nova 版本“2”对设置和功能性 nova 客户端的调用没有 floating_ips 属性。 from novaclient import client as NovaClient clas
如下图所示。当我们从列表中选择记录时,会出现操作下拉列表。但我需要始终显示它,无论用户是否选择了一些记录。 最佳答案 我知道这是旧的,但希望它能帮助别人。 Standalone Actions是你要找
我正在创建一个 Tool为 Laravel Nova 2.0 在我的 Tool我想向 Vue 组件发送一个东西列表: 在 PHP src对于我的工具,我有一个生成“元”的函数,如文档 here 中所建
在查看索引 View 和查看资源的详细 View 时,我想为计算字段返回不同的结果。 基本上类似于下面的 viewIs() : Text::make('Preview', function () {
Nova 计算服务未在 OpenStack 的全新 devstack 安装中运行;报错 nova requires Qemu version 2.1.0 or greater 当我检查版本信息时,我发
我正在使用 Nova 仪表板开发一个项目。出于某种原因,我看不到验证错误消息。 如果存在验证错误,我可以在浏览器控制台中将其视为异常。但不是在 Nova UI 中。 如果表单的所有字段都输入正确,我可
我正在制作 Laravel Nova 应用程序。我正在尝试创建一个 Nova 工具来导入用户。该工具创建得很好,但是当我更新代码时它不显示。 我对此进行了深入研究,问题似乎是 Vendor 文件夹中的
我有一个自定义资源工具在资源的 View 面板中运行良好,但是当我进入编辑模式时它没有出现。有什么我应该添加到组件或 Nova 配置中以在编辑模式下启用组件吗? User.php中的代码 public
我在我的 Nova 应用程序中名为“合作伙伴”的资源中添加了一个 BelongsTo 关系字段(关系名称:用户)。所以在“创建合作伙伴”表单中,现在我有一个选择元素来选择特定用户。 我写的关系包括一个
我有一个名为 Property 的模型,它有一个“事件”标志。我想要在我的资源顶部有一个指标来显示事件属性的数量。 我的计算方法与文档中的完全一样,但这显示了所有属性而不是事件属性: public f
我是一名优秀的程序员,十分优秀!