简体   繁体   English

编辑时显示下拉列表中的其他表数据

[英]Display other table data from drop down list when edit

Is that possible to get data from other table? 是否可以从其他表获取数据?

Let say in create table, I get all the info from genre table and display all data in drop down list 假设在创建表中,我从类型表中获取所有信息,并在下拉列表中显示所有数据

      <select name="mov_genre" id="mov_genre" class="form-control input-sm">
          <option value="">类型</option>
               @foreach(App\Genre::all() as $gData)
          <option value="{{$gData->gen_title}}">{{$gData->gen_title}}</option>
               @endforeach
      </select>

In edit page, I want to display it as well but how to display it as the genre I selected when create? 在编辑页面中,我也要显示它,但是如何将其显示为创建时选择的类型?

In my opinion, you should use $gData->id as value of each option. 我认为,应该将$gData->id用作每个选项的value Because normally you store the id as foreign key and not something like the title. 因为通常您将id存储为外键而不是标题。

But to your question itself: You can simply add an if statement that checks within the foreach loop if the current item is the selected one. 但是对于您自己的问题:您可以简单地添加一个if语句,该语句在foreach循环中检查当前项是否为选定项。 In my example I assume you have a variable $movie which is the entry you want to edit. 在我的示例中,我假设您有一个变量$movie ,这是您要编辑的条目。

  <select name="mov_genre" id="mov_genre" class="form-control input-sm">
      <option value="">-- SELECT GENRE --</option>
      @foreach(App\Genre::pluck('gen_title', 'id') as $key => $value)
          <option value="{{ $key }}" {{ optional($movie)->genre_id === $key ? 'selected' : '' }}>{{ $value }}</option>
      @endforeach
  </select>

Please note that I swapped App\\Genre::all() for App\\Genre::pluck('gen_title', 'id') . 请注意,我将App\\Genre::all()换为App\\Genre::pluck('gen_title', 'id') This will only load the both fields gen_title and id and put them in an array where id is the key and gen_title the value. 这将只加载gen_titleid这两个字段,并将它们放在一个数组中,其中id是键, gen_title是值。 It is done for better performance as we do not load unnecessary data. 这样做是为了提高性能,因为我们不会加载不必要的数据。

Also note that optional($movie) will return null when $movie is not set. 另请注意,未设置$movie时, optional($movie)将返回null This is done to reduce weird if statements and is common practice. 这样做是为了减少怪异的if语句,这是常见的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 CAKEPHP 3 中其他表的下拉列表中显示数据? - HOW TO DISPLAY DATA IN DROP DOWN LIST FROM OTHER TABLE IN CAKEPHP 3? laravel,在另一个表的下拉菜单中显示数据 - laravel, display data on a drop down from another table 如何从动态数据下拉列表中显示选择选项 - How to display select option from dynamic data drop down list 动态将csv文件中的数据显示到下拉列表中 - Dynamically display data from csv file into drop down list 如何在Wordpress中显示从数据库到下拉列表的数据 - How to display data from database to drop-down list in wordpress 在Wordpress页面的下拉列表中显示数据库mySQL中的数据 - display data from database mySQL in a drop down list in a Wordpress page 如何从下拉列表选择中检索数据到表? - How to retrieve data from drop down list selection to a table? 如何使用sql表中的php创建下拉列表,并在下拉列表中显示动态默认表单值 - How to create a drop down list with php from an sql table AND display a dynamic default form value in the drop down list 从数据库中检索值并在php中的编辑表单中显示下拉菜单 - Retrieve value from DB and display in a drop down on edit form in php 从MySQL DB列出表并在下拉列表中将表名称显示为选项 - List tables from MySQL DB and display table names as options in a drop down list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM