【问题标题】:How to make simple dynamic drop list in Laravel?如何在 Laravel 中制作简单的动态下拉列表?
【发布时间】:2019-08-28 05:49:26
【问题描述】:

我是 Laravel 的新手,语法不好。我看过很多教程并阅读了很多答案,但是,我仍然没有明白如何为外键设置下拉字段。

我有一个表“Section”和另一个“Class”。我想在部分页面中显示类的名称。

部分迁移

Schema::create('sections', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->integer('class_id')->unsigned();
    $table->foreign('class_id')->references('id')->on('classses');
});

类迁移

Schema::create('classses', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->timestamps();
});

老实说,我不知道我是否应该更换控制器。

刀片/视图

<div class="form-group">
    <label for="name">Name</label>
    <input type="text" class="form-control" name="name" id="title">
</div>
<div class="form-group">
    <label for="cid">Class</label>
    ???????????????
</div>

索引函数

public function index()
    {        $sections = Section::all();
            return view('sections.index', compact('sections'));

        $classs = Classs::all()->pluck(['id', 'title']);
        return view('sections.index')->with('classs', $classs); }

错误是 $class & Expected string 行的 Unreachable Statement,在 ([id,'title]) 处得到数组

【问题讨论】:

  • 所以你想要一个包含所有课程的下拉菜单?如果是这样,请为您的 classes 表添加迁移 :)
  • 我已经更新了我的问题。请再次检查:)
  • 我编辑了我的答案,看看它是否有效。

标签: php database laravel laravel-5 foreign-keys


【解决方案1】:

在您的控制器中,您有一个返回视图的函数。

将其更改为包含-&gt;with(),以便您可以访问视图中的类:

// if the controller is not for the classes, add this up top:
use App\Classs; // include model name



$classs = Classs:all();

return view('yourView')->with('classe', $classs);

那么,在你看来,你可以这样做:

<div class="form-group">
  <label for="cid">Class</label>

  <select class="" name="cid">
    <option value="null">Class</option>
    @foreach($classs as $class)
      <option value="{{$class->id}}">{{$class->title}}</option>
    @endforeach
  </select>

</div>

它遍历数据库中的所有类并为它们创建一个&lt;option&gt; 元素。查看您的第一次迁移,您正在使用另一个表中的 id,因此您需要将其设置为值。


将您的索引函数更改为:

public function index()
{
    $sections = Section::all();
    $classs = Class::all();

    return view('sections.index')->with('sections', $sections)->with('classs', $classs);
}

你能告诉我在哪里可以写条件,例如 select * from class where role_id=2 等。

基本上,在 MVC 框架中,您在控制器中执行所有查询并将数据传递到视图,在视图中显示数据。

Laravel 有一个 DB class,您可以将其用于基本查询:

select * from class where role_id = 2

在 Laravel 中会变成这个,使用 DB 类。

DB::table('class')->where('role_id', 2)->get();

// or if it's a model:
Model::where('role_id', 2)->get();

【讨论】:

  • 我正在编写索引函数中的上层代码,但由于其中已经有一段代码,它给出了错误。我应该在哪里写这段代码?
  • 将你的索引函数和错误信息添加到帖子中。
  • '公共函数索引()' { $sections = Section::all();返回视图('sections.index',紧凑('sections')); $classs = Classs::all()->pluck(['id', 'title']);返回视图('sections.index')->with('classs', $classs); }'
  • 你能把它加到帖子里吗,让它更容易阅读。
  • 它有效,但你能告诉我在哪里可以写条件,例如 select * from class where role_id=2 等。
【解决方案2】:

我现在已经在刀片页面中使用了这个代码

              @php
                use Illuminate\Support\Facades\DB;
                $cid=DB::table('classses')->get();
                $uid=DB::table('users')->where('role_id','3')->get();
                $counter=0;
                @endphp

                <select class="" name="class_id">
                    <option value="null">Class</option>
                    @foreach($cid as $class)
                        @if($counter==0)
                        <option selected="selected" value="{{$class->id}}">{{$class->title}}</option>
                        {{$counter++}}
                    @else

                        <option value="{{$class->id}}">{{$class->title}}</option>
                    @endif
                            @endforeach
                </select>

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 2011-09-22
    • 2020-04-13
    • 2015-01-19
    • 1970-01-01
    • 2016-08-25
    • 2015-05-26
    • 1970-01-01
    • 2020-06-29
    相关资源
    最近更新 更多