我是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行和期望的字符串中的Unreachable语句,数组位于[[id,'title])
我现在在刀片页面中使用了此代码
@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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句