我正在与Laravel合作开发一个项目,希望使用dropdownlist并使用数据库中的数据填充它,请提供帮助。这是我使用的代码:
控制器:
public function create()
{
$items = Income::pluck('name', 'id');
return view ('IncomeExpense.create');
}
查看:
<div class="form-group">
{{Form::label('', 'Category')}}
{{Form::select('IncomeExpense',null, $items,['class'=>'form-control',
'placeholder'=>'Choose category'])}}
</div>
答案 0 :(得分:2)
Mirasan-您需要通过视图,以便从数据库中检索您的信息。
$items = Income::pluck('name', 'id');
return view ('IncomeExpense.create')->with(compact('items'));
然后在刀片文件中,您将访问数组'items'(而不是$ items)。
致谢-
答案 1 :(得分:1)
首先
您的控制器有错误...请阅读eloquent official documentation,应为:
public function create()
{
$items = Income::all();
return view ('IncomeExpense.create', compact('items'));
}
无论如何,您都不应使用表单构建器,如果您使用Components & Slots之类的本机刀片功能,它会更有用。如果我正确地注册,可以从Laravel 5.0中删除Form Builder,以便将框架的注意力集中在后端...
在Bootstrap 4中有两个示例:
香草
<form method="POST" action="{{ route('your-route') }}" aria-label="{{ __('My form') }}">
@csrf
<div class="form-group row">
<label for="dropdown" class="col-sm-4 col-form-label text-md-right">{{ __('My dropdown') }}</label>
<div class="col-md-12">
<select class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="dropdown">
@foreach($my_collection as $item)
<option value="{{ $item->id }}">{{ $item->text }}</option>
@endforeach
</select>
@if ($errors->has('dropdown'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('dropdown') }}</strong>
</span>
@endif
</div>
</div>
{{-- other fields --}}
组件
<!-- /resources/views/components/select.blade.php -->
{{ $label }}
<div class="col-md-{{ $column == null ? 12 : $column }}">
<select class="form-control{{ ' ' . $select_css }}{{ $error == null ? '' : ' is-invalid' }}" name="dropdown">
@foreach($my_collection as $item)
<option value="{{ $item->id }}">{{ $item->text }}</option>
@endforeach
</select>
@if ($error != null)
<span class="invalid-feedback" role="alert">
<strong>{{ $error</strong>
</span>
@endif
</div>
<!-- /resources/views/my/form.blade.php -->
<form method="POST" action="{{ route('your-route') }}" aria-label="{{ __('My form') }}">
@csrf
<div class="form-group row">
@component('components.select', ['select_css' => 'whatever', 'my_collection' => $my_collection])
@slot('label')
<label for="dropdown" class="col-sm-4 col-form-label text-md-right">{{ __('My dropdown') }}</label>
@endslot
</div>
{{-- other fields --}}
答案 2 :(得分:0)
**fetch_data.blade.php**
<!DOCTPE html>
<html>
<head>
<title>View Student Records</title>
</head>
<body>
<form action="search/" method="get" role="search">
{{ csrf_field() }}
<div class="input-group">
<input type="search" name="search" class="form-control" placeholder="Search users"> <span class="input-group-prepend">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search">Search</span>
</button>
</span>
</div>
<table border = "1">
<tr>
<td>Id</td>
<td>First Name</td>
<td>Last Name</td>
<td>City Name</td>
<td>Email</td>
<td>Id</td>
<td>First Name</td>
<td>Last Name</td>
<td>Delete</td>
<td>Edite</td>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->COUNTRY_CODE }}</td>
<td>{{ $user->STATE_CODE }}</td>
<td>{{ $user->CITY_CODE }}</td>
<td>{{ $user->PIN_CODE }}</td>
<td>{{ $user->AREA_CODE }}</td>
<td>{{ $user->AREA_SERIAL }}</td>
<td>{{ $user->AREA_DESCRIPTION}}</td>
<td>{{ $user->AREA_DESCRIPTION_ARABIC}}</td>
<!-- <td><a href = 'delete/ {{ $user->AREA_SERIAL}}'>Delete</a></td> -->
<td><a href = 'edit/{{ $user->AREA_SERIAL }}'>Edit</a></td>
<td><a class="btn btn-danger" onclick="return confirm('Are you sure?')" href='delete/ {{ $user->AREA_SERIAL}}'>Delete<i class="fa fa-trash"></i></a></td>
</tr>
@endforeach
</table>
</form>
</body>
</html>
**web.php**
Route::get('/fetchre','StudViewController@index');
Route::get('/delete/{AREA_SERIAL}','StudViewController@destroy');
Route::get('edit/{AREA_SERIAL}','StudViewController@show');
Route::post('update/{AREA_SERIAL}','StudViewController@update');
**update.blade.php**
<!DOCTYPE html>
<html>
<head>
<title>Student Management | Edit</title>
</head>
<body>
<form action ="update/{{ $users[0]->AREA_SERIAL }}" method = "post">
{{csrf_field()}}
<div class="form-group">
<label>First Name</label>
<input type = 'text' name ="COUNTRY_CODE"
value ="{{ $users[0]->COUNTRY_CODE }}"/>
</div>
<div class="form-group">
<label>Last Name</label>
<input type = 'text' name ="STATE_CODE"
value ="{{ $users[0]->STATE_CODE }}"/>
</div>
<div class="form-group">
<label>City Name</label>
<input type = 'text' name = "CITY_CODE"
value = '{{ $users[0]->CITY_CODE }}'/>
</div>
<div class="form-group">
<label>Email</label>
<input type = 'text' name ="PIN_CODE"
value = '{{ $users[0]->PIN_CODE }}'/>
</div>
<div class="form-group">
<label>Email</label>
<input type = 'text' name = "AREA_CODE"
value = '{{ $users[0]->AREA_CODE }}'/>
</div>
<div class="form-group">
<label>Email</label>
<input type = 'text' name ="AREA_DESCRIPTION"
value = '{{ $users[0]->AREA_DESCRIPTION }}'/>
</div>
<div class="form-group">
<label>Email</label>
<input type = 'text' name = "AREA_DESCRIPTION_ARABIC"
value = '{{ $users[0]->AREA_DESCRIPTION_ARABIC }}'/>
</div>
<div class="form-group">
<input type = 'submit' value = "Update student" />
</div>
</form>
</body>
</html>
i try to update my data but its going to 404 not found page i dont no y ??
plz any buddy help me