laravel - 为 laravel nova 上的数据透视表属性创建更新操作 belongsToMany 关系
问题描述
我的模型看起来像下面的Product
模型代码:
public class Product extends Model
{
public function types()
{
return $this->belongsToMany(Type::class)
->withPivot('published');
}
}
这里是Types
模型:
public class Type extends Model
{
public function products()
{
return $this->belongsToMany(Product::class)
->withPivot('published');
}
}
在 novaProjectResource
我有以下字段:
BelongsToMany::make('Types')
->fields(function() {
return [
Boolean::make('Published')
];
})->actions(function() { return new Action/UpdateProductTypeActions }),
在 novaTypeResource
我有以下字段:
BelongsToMany::make('Projects')
->fields(function() {
return [
Boolean::make('Published')
];
}),
我想让published
数据透视表中的这个属性可以使用Nova Actions
我已经创建UpdateProductTypeActions
了如下代码:
public function handle(ActionFields $fields, Collection $models)
{
foreach ($models as $model) {
}
}
我的问题如何product_id
从这些行动中得到?
解决方案
这将为您提供与与 $model 具有多对多关系的类型具有多对多关系的所有产品,这是您的项目:
foreach($model->types as $type)
{
$products=$type->products->pluck(id);
}
推荐阅读
- square-connect - 如何将 squareup 开发者帐户与商家帐户结合起来
- c++ - 如何初始化多种类型对象的自动类型数组
- c# - 当前上下文中不存在 Print() 名称
- bash - 如何在 awk 中使用命令行参数?
- java - 如何保持原生对象处于状态?
- php - 如何在第二个ajax完成后选择输入选项,JQuery
- php - 未选择文件时如何使输入类型文件发送空字符串而不是null
- shell - 如何排除匹配行及其前面的行?
- java - mapstruct - 将父字段值传播到嵌套对象的集合
- react-native - Native-base 不能与 react-native 0.57.1 一起使用?