Laravel:从数据库显示 DropDown 中的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51878335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:58:56  来源:igfitidea点击:

Laravel: Display data in DropDown from database

phplaravellaravel-5.6

提问by Mirasan

I was developing a project with Laravel and want to use dropdownlist and populate it with data from database please help. Here is the code that I use:

我正在用 Laravel 开发一个项目,想使用下拉列表并用数据库中的数据填充它,请帮忙。这是我使用的代码:

Controller:

控制器:

 public function create()
 {
    $items = Income::pluck('name', 'id');
    return view ('IncomeExpense.create');
 }

View:

看法:

 <div class="form-group">
     {{Form::label('', 'Category')}}
     {{Form::select('IncomeExpense',null, $items,['class'=>'form-control',
     'placeholder'=>'Choose category'])}}
 </div>

回答by IlGala

First of all,

首先,

you have an error in your controller... Please read the eloquent official documentationIt should be:

你的控制器有错误......请阅读雄辩的官方文档它应该是:

public function create()
 {
    $items = Income::all();
    return view ('IncomeExpense.create', compact('items'));
 }

Anyway you should not use a form builder, I think it's much more usefull if you use the native blade features like Components & Slots. Form Builder have been removed from Laravel 5.0 if I rember correctly in order to focus the framework attention to the backend...

无论如何,您不应该使用表单构建器,我认为如果您使用诸如Components & Slots 之类的本机刀片功能,它会更有用。如果我记得正确,Form Builder 已从 Laravel 5.0 中删除,以便将框架注意力集中在后端......

Heare a couple of example with Bootstrap 4:

下面是 Bootstrap 4 的几个例子:

Vanilla

香草

<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 --}}

Components

成分

<!-- /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 --}}

回答by Mark C.

Mirasan - You need to pass the view your information retrieved from the database.

Mirasan - 您需要传递从数据库中检索到的信息的视图。

$items = Income::pluck('name', 'id');
return view ('IncomeExpense.create')->with(compact('items'));

Then inside your blade file you will access the array 'items' (rather than $items).

然后在您的刀片文件中,您将访问数组“items”(而不是 $items)。

Regards -

问候 -

回答by Mohammed Farooque Shaikh

**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

enter image description here

在此处输入图片说明