Php 如何从编辑页面laravel的下拉列表中获取保存的值?
在Php 如何从编辑页面laravel的下拉列表中获取保存的值?,php,laravel-7,Php,Laravel 7,在EmployeeController的edit功能中,我有这个代码 public function edit($id) { $employees = Employee::find($id); $departmentlists = Department::pluck('id', 'name'); return view('employees.edit', compact('employees', 'departmentlists')); } {!
EmployeeController
的edit
功能中,我有这个代码
public function edit($id)
{
$employees = Employee::find($id);
$departmentlists = Department::pluck('id', 'name');
return view('employees.edit', compact('employees', 'departmentlists'));
}
{!! 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() !!}
在edit.blade.php
中显示下拉列表
我有这个代码
public function edit($id)
{
$employees = Employee::find($id);
$departmentlists = Department::pluck('id', 'name');
return view('employees.edit', compact('employees', 'departmentlists'));
}
{!! 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() !!}
{!!Form::open(['action'=>['EmployeeController@update“,$employees->id],“method'=>“POST”,“autocomplete'=>“off”,“class'=>“form horizontal”,“enctype'=>“application/x-www-form-urlencoded'])
“卡片标题”])}
{{Form::label('Name','',['class'=>'col-sm-2 col-Form label'])}
{{Form::text('name',$employees->name,['class'=>'表单控件','placeholder'=>'name','required'])}
@error('name'){{{$message}}@enderror
{{Form::label('Department','',['class'=>'col-sm-2 col-Form label'])}
@foreach($dl形式的部门列表)
id==$employees->department_id)selected=='selected'@endif>{{{$employees->name}
@endforeach
@error('id'){{{$message}}@enderror
{{Form::label('Keterangan','',['class'=>'col-sm-2 col-Form label'])}
{Form::textarea('information',$employees->information,['class'=>'Form control','placeholder'=>'Keterangan','rows'=>'6','cols'=>'50'])}}
{{Form::hidden('u method','PUT')}
{{表单::提交('Ubah',['class'=>'btn btn primary'])}
{!!Form::close()!!}
这是
这是
现在,目标是我希望编辑页面上的下拉列表显示员工所属的部门名称,而下拉列表中仍然包含所有部门名称。
所以我可以改变它,但是当我运行这个代码时,它会给我这个错误
正在尝试获取非对象的属性“id”(视图:
C:\xampp\htdocs\ims-it-laravel7\resources\views\employees\edit.blade.php)
我在这里读过其他线程,但这些代码仍然不能解决问题
$departmentlists=Department::pull('id','name')代码>
以后你用
@foreach($departmentlist作为$dl)
和$dl->id
$dl
不是一个对象,它是一个数组,因为有pluck()
函数
更确切地说,它看起来像这样
[
"abc" => 1
"xyz" => 2
"foo" => 3
]
注意:要亲自查看,请尝试使用dd($departmentlists)
或dump($departmentlists)
请看
在这种情况下,您可能需要使用Department::select(['id',name'])->get()
,因为它将返回具有指定属性的对象集合。$departmentlists=Department::pull('id',name')代码>
以后你用
@foreach($departmentlist作为$dl)
和$dl->id
$dl
不是一个对象,它是一个数组,因为有pluck()
函数
更确切地说,它看起来像这样
[
"abc" => 1
"xyz" => 2
"foo" => 3
]
注意:要亲自查看,请尝试使用dd($departmentlists)
或dump($departmentlists)
请看
在这种情况下,您可能需要使用Department::select(['id','name'])->get()
,因为它将返回具有指定属性的对象集合。为什么有选项标记而没有选择标记?为什么不使用Forn::model()?为什么有选项标记而没有选择标记?为什么不使用Forn::model()?