如何从编辑页面 laravel 的下拉列表中获取保存的值?
How to get saved value from dropdown in edit page laravel?
在edit
函数的EmployeeController
里面,我有这段代码
public function edit($id)
{
$employees = Employee::find($id);
$departmentlists = Department::pluck('id', 'name');
return view('employees.edit', compact('employees', 'departmentlists'));
}
并在 edit.blade.php
内显示 dropdown
我有这个代码
{!! Form::open(['action' => ['EmployeeController@update', $employees->id], 'method' => 'POST', 'autocomplete' => 'off', 'class' => 'form-horizontal', 'enctype' => 'application/x-www-form-urlencoded']) !!}
<div class="card">
<div class="card-header card-header-primary">
<h4 {{ Form::label('', 'Change Employee Data', ['class' => 'card-title']) }}
</h4>
<p class="card-category"></p>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12 text-right">
<a href="{{ route('employees.index') }}" class="btn btn-sm btn-primary" style="font-size:12px">
<i class="material-icons">keyboard_backspace</i>
{{ __('kembali') }}
</a>
</div>
</div>
<div class="row">
{{ Form::label('Name', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="form-group col-sm-7">
{{Form::text('name', $employees->name, ['class' => 'form-control', 'placeholder' => 'Name', 'required'])}}
<p style="color: red;">@error('name') {{ $message }} @enderror</p>
</div>
</div>
<div class="row">
{{ Form::label('Department', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="col-md-7">
<div class="form=group">
@foreach($departmentlists as $dl)
<option value="{{ $dl->id }}" @if($dl->id==$employees->department_id) selected='selected' @endif >{{ $employees->name }}</option>
@endforeach
<p style="color: red;">@error('id') {{ $message }} @enderror</p>
</div>
</div>
</div>
<div class="row">
{{ Form::label('Keterangan', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="form-group col-sm-7">
{{ Form::textarea('information', $employees->information, ['class' => 'form-control', 'placeholder' => 'Keterangan', 'rows' => '6', 'cols' => '50']) }}
</div>
</div>
</div>
<div class="card-footer ml-auto mr-auto">
{{ Form::hidden('_method','PUT') }}
{{ Form::submit('Ubah', ['class' => 'btn btn-primary']) }}
</div>
</div>
{!! Form::close() !!}
这是employees table
这是 departments table
现在,我的目标是让编辑页面上的下拉菜单显示员工所属的部门名称,而下拉菜单仍然包含所有部门名称。
所以我可以更改它,但是当我 运行 这段代码时它给了我这个错误。
Trying to get property 'id' of non-object (View:
C:\xampp\htdocs\ims-it-laravel7\resources\views\employees\edit.blade.php)
我已经阅读了这里的其他线程,但是那些代码仍然没有解决问题
$departmentlists = Department::pluck('id', 'name');
以后你用
@foreach($departmentlists as $dl)
和 $dl->id
$dl
不是一个对象,它是一个数组,因为 pluck()
函数。
更准确地说,它看起来像这样
[
"abc" => 1
"xyz" => 2
"foo" => 3
]
注意:要亲自查看,请尝试使用 dd($departmentlists)
或 dump($departmentlists)
在这种情况下,您可能希望使用 Department::select(['id', 'name'])->get()
,因为它将 return 具有指定属性的对象集合。
在edit
函数的EmployeeController
里面,我有这段代码
public function edit($id)
{
$employees = Employee::find($id);
$departmentlists = Department::pluck('id', 'name');
return view('employees.edit', compact('employees', 'departmentlists'));
}
并在 edit.blade.php
内显示 dropdown
我有这个代码
{!! Form::open(['action' => ['EmployeeController@update', $employees->id], 'method' => 'POST', 'autocomplete' => 'off', 'class' => 'form-horizontal', 'enctype' => 'application/x-www-form-urlencoded']) !!}
<div class="card">
<div class="card-header card-header-primary">
<h4 {{ Form::label('', 'Change Employee Data', ['class' => 'card-title']) }}
</h4>
<p class="card-category"></p>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12 text-right">
<a href="{{ route('employees.index') }}" class="btn btn-sm btn-primary" style="font-size:12px">
<i class="material-icons">keyboard_backspace</i>
{{ __('kembali') }}
</a>
</div>
</div>
<div class="row">
{{ Form::label('Name', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="form-group col-sm-7">
{{Form::text('name', $employees->name, ['class' => 'form-control', 'placeholder' => 'Name', 'required'])}}
<p style="color: red;">@error('name') {{ $message }} @enderror</p>
</div>
</div>
<div class="row">
{{ Form::label('Department', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="col-md-7">
<div class="form=group">
@foreach($departmentlists as $dl)
<option value="{{ $dl->id }}" @if($dl->id==$employees->department_id) selected='selected' @endif >{{ $employees->name }}</option>
@endforeach
<p style="color: red;">@error('id') {{ $message }} @enderror</p>
</div>
</div>
</div>
<div class="row">
{{ Form::label('Keterangan', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="form-group col-sm-7">
{{ Form::textarea('information', $employees->information, ['class' => 'form-control', 'placeholder' => 'Keterangan', 'rows' => '6', 'cols' => '50']) }}
</div>
</div>
</div>
<div class="card-footer ml-auto mr-auto">
{{ Form::hidden('_method','PUT') }}
{{ Form::submit('Ubah', ['class' => 'btn btn-primary']) }}
</div>
</div>
{!! Form::close() !!}
这是employees table 这是 departments table
现在,我的目标是让编辑页面上的下拉菜单显示员工所属的部门名称,而下拉菜单仍然包含所有部门名称。 所以我可以更改它,但是当我 运行 这段代码时它给了我这个错误。
Trying to get property 'id' of non-object (View:
C:\xampp\htdocs\ims-it-laravel7\resources\views\employees\edit.blade.php)
我已经阅读了这里的其他线程,但是那些代码仍然没有解决问题
$departmentlists = Department::pluck('id', 'name');
以后你用
@foreach($departmentlists as $dl)
和 $dl->id
$dl
不是一个对象,它是一个数组,因为 pluck()
函数。
更准确地说,它看起来像这样
[
"abc" => 1
"xyz" => 2
"foo" => 3
]
注意:要亲自查看,请尝试使用 dd($departmentlists)
或 dump($departmentlists)
在这种情况下,您可能希望使用 Department::select(['id', 'name'])->get()
,因为它将 return 具有指定属性的对象集合。