简体   繁体   English

Laravel选择具有默认值的框

[英]Laravel select box with default value

I have a category/subcategory setup on my application in a way that each category can have parent_id which is 0 if we have main category, or is number pointing to a main category if it is a subcategory. 我在我的应用程序上有一个类别/子类别设置,每个类别可以有parent_id ,如果我们有主要类别,则为0,或者如果它是子类别,则指向主类别的数字。

Everything works fine except the edit method where I'd like a select box to have "Main Category" on top of the list able to select. 一切正常,除了编辑方法,我想要一个选择框,在列表顶部可以选择“主类别”。

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'));
}

If I prepend it like this it works, but then in view when I edit a category that value is always selected even though category points to another parent_id . 如果我像这样预先设置它可以工作,但是当我编辑一个类别时,即使类别指向另一个parent_id ,也始终选择该值。 When I don't have prepend() then it selects value fine. 当我没有prepend()然后它选择值罚款。 This is view 这是观点

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

EDIT: 编辑:

actually it doesn't select it fine in either case 实际上它在任何一种情况下都没有选择它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM