我是beiginer。现在我明白了如何制作选择框。如下。 我想创建另一个选择框字段作为产品名称。 它的约100个产品很长。 什么是将许多数据添加到index.blade.php的智能和最佳方式? 我想把它作为独立文件。
index.blade.php
@extends('layouts.app')
@section('content')
<div class="form-group{{ $errors->has('age') ? ' has-error' : '' }}">
<p>Age</p>
{{ Form::select('age', ['Under 18', '19 to 30', 'Over 30']) }}
<div class="col-sm-10">
@if ($errors->has('age'))
<span class="help-block">
<strong>{{ $errors->first('age') }}</strong>
</span>
@endif
</div>
@endsection
这是列表示例
a1234
b1254
c546
d6467
e8952
f84664
g45646
h46546
.
.
.
答案 0 :(得分:0)
在Blade中使用@foreach
<select name="product">
@foreach($products as $product)
<option value="{{$option->id}}">{{$option->name}}</option>
@endforeach
</select>
答案 1 :(得分:0)
我会创建另一个像select_products.blade.php
这样的刀片
在其中,你可以有类似的东西:
{{ Form::select('products', $productsArray) }}
您可以使用pluck
方法以正确的格式获取数组。就像这样:
$productsArray = ProductModel::all()->pluck('name', 'id)->all();
理想情况下,应该在将数据发送到视图(可能在您的控制器中)之前完成
你可以使用Blade的@include指令来调用select_products.blade.php
希望它有所帮助,o /
考虑到很多产品,你也可以使用类似select2的东西来获得更好的用户体验