[英]How to pass value from model to select input use laravel eloquent
I have model with attributes category i want pass 1 data to select tag我有属性类别的模型我想传递 1 个数据来选择标签
Category option类别选项
Category
1. Fruit
2. Vegetables
3. Meat
example data示例数据
name![]() |
category![]() |
---|---|
Apple![]() |
1 ![]() |
how i can pass apple category to select tag我如何通过苹果类别来选择标签
<select class="form-control form-select" name="category" aria-label="Default select example">
<option value=""></option>
<option value="1">fruit</option>
<option value="2">vegetables</option>
<option value="3">meat</option>
</select>
You didn't provide much details but I guess you meant something like this:你没有提供太多细节,但我猜你的意思是这样的:
In your controller action在您的控制器操作中
$categories = Category::pluck(‘name’, ‘id’)->toArray();
And in your view在你看来
<select class="form-control form-select" name="category" aria-label="Default select example">
<option value=""></option>
@foreach($categories as $key => $category)
<option value="{{ $key }}">{{ $name }}</option>
@endforeach
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.