Laravel选择具有默认值的框

时间:2017-05-18 07:47:47

标签: php laravel

我的应用程序中有一个类别/子类别设置,每个类别都可以parent_id,如果我们有主要类别,则为0;如果是子类别,则指向主要类别的数字。< / p>

一切正常,除了编辑方法,我想要一个选择框,在列表顶部可以选择“主要类别”。

public function edit(Category $category)
{
    $parent_categories = Category::where('parent_id', 0)->pluck('name', 'id')->prepend('Main Category', '');
    return view('category.edit', compact('category', 'parent_categories'));
}

如果我像这样预先设置它可以工作,但是当我编辑一个类别时,即使类别指向另一个parent_id,也始终选择该值。当我没有prepend()时,它会选择正确的值。这是视图

{!! Form::select('parent_id', $parent_categories, null, ['class' => 'form-control'] ) !!}

修改

实际上在两种情况下都没有选择它

1 个答案:

答案 0 :(得分:3)

just replace $selctedValue as the value to be selected in the below code .
{!! Form::select('parent_id', $parent_categories, $selctedValue, ['class' => 'form-control'] ) !!}
相关问题