首页 文章

Laravel Nova - 从BelongsToMany字段隐藏视图和编辑

提问于
浏览
0

模型 QuestionChoice 之间的关系是多对多的 .

Nova/Question

public function fields(Request $request)
{
    return [
        ...
        BelongsToMany::make('Choices')
    ]
}

哪个显示在屏幕下方

enter image description here

我想从视图中隐藏视图和编辑选项 . 仅允许用户附加/分离选择 .

我尝试使用以下方法创建 QuestionPolicy ,但无效 . 如果我使用 ChoicePolicyupdatecreate 方法,它将隐藏 Choice 资源中的查看和编辑选项 .

public function updateChoice(User $user, Question $question)
{
    return false;
}

如何从关系中实现隐藏视图和编辑选项?

Update

Question 型号

class Question extends Model
{
    ...
    /**
     * The choices that belong to the question.
     */
    public function choices()
    {
        return $this->belongsToMany('App\Choice');
    }
}

Choice 型号

class Choice extends Model
{
    /**
     * The questions that belong to the choice.
     */
    public function questions()
    {
        return $this->belongsToMany('App\Question');
    }
}

3 回答

  • 0

    我认为您需要将以下代码放在QuestionPolicy中:

    public function attachAnyChoice()
    {
       return false;
    }
    

    希望这有帮助 . https://nova.laravel.com/docs/1.0/resources/authorization.html#authorizing-attaching-detaching

  • 0

    希望对你有帮助

    public function fields(Request $request)
    {
         return [
            ...
            BelongsToMany::make('Choices')->except('edit','view');
         ]
    }
    
  • 0

    您可以在您的政策中退回此内容,同样适用于您的更新

    public function view(User $user, Survey $survey)
         {
             return false
          }
    

相关问题