【问题标题】:selected value tag of Form::model doesn't bind property from ModelForm::model 的选定值标签不绑定 Model 的属性
【发布时间】:2015-07-05 16:51:29
【问题描述】:

我正在学习 Laravel,在尝试将模型的属性绑定到选择标记的选定值时遇到问题。我试图将第三个参数保留为 null,因为我相信 Form Model Binding 会自动处理它,但它不起作用。这是我已经尝试过的:

   {{$article->tag_list}} // show [1,3]

    //it doesn't work 
   {!! Form::select('tag_list[]', $tags, null , ['class' => 'form-control', 'multiple'] ) !!}
    -------------

    //it doesn't work as well
    {!! Form::select('tag_list[]', $tags, $article->tag_list  , ['class' => 'form-control', 'multiple'] ) !!}
    -----------

    //it works
    {!! Form::select('tag_list[]', $tags, [1,3] , ['class' => 'form-control', 'multiple'] ) !!}

在模型中,我有 getTagListAttribute() 工作正常。

public function getTagListAttribute(){
    return $this->tags->lists('id');
}

使用文本输入,表单可以正常工作。顺便说一句,我使用的是 5.2.1 版本。我在这里错过了什么?

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    我找到了丢失的部分。 select 函数需要一个数组,但 getTagListAttribute() 返回一个 Collection 对象。

    public function getTagListAttribute(){
      return $this->tags->lists('id')->all();
    }
    or I can do this
    public function getTagListAttribute(){
      return $this->tags->lists('id')->toArray();
    }
    

    【讨论】:

    • 如果你在 5.1 $this->tags->lists('id') 已更改为$this->tags->lists('id')->all()
    • 谢谢。这是救命稻草。有人需要更新 laracast 视频上的这些信息laracasts.com/series/laravel-5-fundamentals/episodes/22
    • 在 laravel 5.3 中函数 list() 改为 pluck()
    猜你喜欢
    • 2015-06-06
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    相关资源
    最近更新 更多