我正在尝试填充选择下拉列表,但没有看到值的结果。
控制器:
public function create()
{
$employees = Employee::get();
$am = DB::table('employees')
->where('department', 'Account Manager')
->select('id', 'name')
->orderBy('name', 'asc')
->get();
$pm = DB::table('employees')
->where('department', 'Project Manager')
->select('id', 'name')
->orderBy('name', 'asc')
->get();
$employees->am = $am;
$employees->pm = $pm;
return view('projects/create', [
'employees' => $employees
]);
}
查看:
<div class="form-group">
<label>Account Manager</label>
<select class="form-control" name="am">
<option value=""></option>
@foreach ($employees as $employee)
<option value="{{ $employee->am['id'] }}">{{ $employee->am['name'] }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label>Project Manager</label>
<select class="form-control" name="pm">
<option value=""></option>
@foreach ($employees as $employee)
<option value="{{ $employee->pm['id'] }}">{{ $employee->pm['name'] }}</option>
@endforeach
</select>
</div>
所以,我知道有些东西正在运行,因为下拉列表显示有5个空格,它们是数据库中的记录数,但它们都是空白的。执行var_dump会将其显示为NULL。此外,它没有考虑WHERE子句,因为两个下拉列表显示5个空格而不是3个用于项目经理,2个用于客户经理。
我的模型没有任何关系设置。它有吗?因为我为一个不同的视图做了一个连接,它运行得很好。
我的数据库表格如下所示:
项目:
id
name
am_id
pm_id
雇员:
id
name
department
如何在下拉列表中填充值?
答案 0 :(得分:0)
$employeeAm = Employee::where('department', 'Account Manager')
->select('id', 'name')
->orderBy('name', 'asc')
->get();
$employeePm = Employee::where('department', 'Project Manager')
->select('id', 'name')
->orderBy('name', 'asc')
->get();
然后在您的视图中传递给您的视图$ employeeAm和$ employeePm
@foreach ($employeePm as $employee)
<option value="{{ $employee->id }}">{{ $employee->name }}</option>
@endforeach
与您的$employeeAm