Laravel-5 如何使用 id 值和名称值从数据库中填充选

  
本文介绍了Laravel-5 如何使用 id 值和名称值从数据库中填充选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 illuminatehtml 创建一个如下所示的选择框:

<option value="$item->id">$ite​​m->name</option><option value="$item->id">$ite​​m->name</option></选择>在我的控制器中我试过这个:公共函数create(){$items = Items::all(['id', 'name']);return view('prices.create', compact('id', 'items'));}在我看来:{!!表格::标签('项目', '项目:') !!}{!!Form::select('item_id', $items, null, ['class' => 'form-control']) !!}问题是显示实体的所有信息而不是 $item->name. 解决方案 Laravel 提供了一个带有 list() 函数的查询构建器在你的情况下,你可以替换你的代码$items = Items::all(['id', 'name']);与$items = Items::lists('name', 'id');此外,您也可以将其与其他查询生成器链接.$items = Items::where('active', true)->orderBy('name')->lists('name', 'id');来源:http://laravel.com/docs/5.0/queries#selects<小时>Laravel 5.2 更新 非常感谢@jarry.正如你提到的,Laravel 5.2 的功能应该是$items = Items::pluck('name', 'id');或$items = Items::where('active', true)->orderBy('name')->pluck('name', 'id');ref: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0 -- 查看弃用列表I want to create a select box like the one below using illuminatehtml :<select>
    <option value="$item->id">$item->name</option>
    <option value="$item->id">$item->name</option>
</select>
In my controller I tried this:public function create()
{
    $items = Items::all(['id', 'name']);

    return view('prices.create', compact('id', 'items'));
}
And in my view this:<div class="form-group">
    {!! Form::Label('item', 'Item:') !!}
    {!! Form::select('item_id', $items, null, ['class' => 'form-control']) !!}
</div>
The issue is that instead of $item->name is displaying all the info of the entity.  解决方案 Laravel provides a Query Builder with lists() function

In your case, you can replace your code$items = Items::all(['id', 'name']);
with$items = Items::lists('name', 'id');
Also, you can chain it with other Query Builder as well.$items = Items::where('active', true)->orderBy('name')->lists('name', 'id');
source: http://laravel.com/docs/5.0/queries#selects



Update for Laravel 5.2 

Thank you very much @jarry. As you mentioned, the function for Laravel 5.2 should be$items = Items::pluck('name', 'id');
or$items = Items::where('active', true)->orderBy('name')->pluck('name', 'id');
ref: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0  -- look at Deprecations lists

                        这篇关于Laravel-5 如何使用 id 值和名称值从数据库中填充选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
                    
相关文章
php问题最新文章 PHP Fatal error: Cannot pass parameter 2 by reference解决办法 PHP Fatal error: Cannot pass parameter N by reference in错误原因及解决办法 在WooCommerce结账中添加带有收集时间的自定义选择字段 在WooCommerce中应用特定优惠券代码时向客户发送电子邮件通知 在WooCommerce结账前的强制优惠券(可选:针对特定产品) 根据购物车合计(小计+运费)计算费用,而不将其添加到WooCommerce中的订单总价值 PHP致命错误:无法将词法变量$EventName用作中的参数名 我试图使用PHP代码调用POST请求,但收到了401个未经授权的错误。 在WooCommerce的管理订单编辑页面上获取嵌套自定义字段元数据 根据之前在WooCommerce上的订单,限制客户在同一周内多次购买特定产品 Symfony中的路由异常(&quot;The&quot;/League/:ID_League/Members/new&quot;Routing有一些缺少的必选参数(:ID_League)&quot;) 尝试访问Microsoft Graph时无法获取本地颁发者证书(&Q) 热门教程 HTML/CSS教程JAVASCRIPT教程PHP教程JAVA教程ASP.NET教程Python教程Go教程Ruby教程C教程C++教程
© 2023-2024 沃梦达编程网 版权所有并保留所有权 ICP备案号:粤ICP备14083021号 SiteMap 本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!