【问题标题】:Laravel Nova nested relationLaravel Nova 嵌套关系
【发布时间】:2019-06-18 23:55:01
【问题描述】:

最近我一直在尝试创建依赖于为主要资源提供信息的其他资源的 Nova 资源。

我有一个表 contest_entries,其中包含以下字段:

  • 身份证
  • contest_id
  • user_id

有以下关系

    public function contest() : BelongsTo {
        return $this->belongsTo(Contest::class, 'contest_id', 'id');
    }

    public function user() : BelongsTo {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

另外,我有一个表contest_submissions,其中包含以下字段:

  • 身份证
  • entry_id
  • task_id
  • 评论
  • 已批准
  • 拒绝 具有以下关系:
public function entry() : BelongsTo {
    return $this->belongsTo(ContestEntry::class, 'entry_id', 'id');
}

public function user() : BelongsTo {
    return $this->entry->user();
}

public function contest() : BelongsTo {
    return $this->entry->contest();
}

public function task() : BelongsTo {
    return $this->belongsTo(Task::class, 'task_id', 'id');
}

我在 Nova 的 indexdetails 视图上获取这些数据没有问题,一切都“正常工作”,但是,当我尝试更新资源时,我收到了 user() 的错误或contest()null 上调用。

我尝试了以下方法,

return [
    BelongsTo::make('Contest', 'contest', Contests::class)->exceptOnForms(),
    BelongsTo::make('Task', 'task', ContestTasks::class)->exceptOnForms(),
    BelongsTo::make('User', 'user', AccountUsers::class)->exceptOnForms(),
]

但由于某种原因,当我明确告诉它不要时,Nova 仍在尝试获取这些关系。

非常感谢任何想法,因为它适用于任何地方,除了update 视图(create 视图被明确禁用,因为提交是由用户在前端创建的)

【问题讨论】:

    标签: php laravel laravel-nova


    【解决方案1】:

    您还应该将hideWhenUpdating() 约束链接到它。

    return [
        BelongsTo::make('Contest', 'contest', Contests::class)
            ->hideWhenUpdating()
            ->exceptOnForms(),
    
        BelongsTo::make('Task', 'task', ContestTasks::class)
            ->hideWhenUpdating()
            ->exceptOnForms(),
    
        BelongsTo::make('User', 'user', AccountUsers::class)
            ->hideWhenUpdating()
            ->exceptOnForms(),
    ]
    

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-20
      • 2021-08-05
      • 2020-11-22
      相关资源
      最近更新 更多