Laravel 8 Livewire 在下拉列表中保存所选值的 ID
Laravel 8 Livewire Save ID of Selected Value in Drop Down
我的数据库中有两个表 "emp_info" 包含(user_id、名字、姓氏) 和 "load_schedule" contains(faculty_id, corsname, day, room, time).
问题是我有一个编辑模式(弹出窗口),它包含在我的主视图中,尽管它显示了讲师列表 ($instructors) 但它无法更新 faculty_id 具有新选定值的列。
到目前为止试过这个:
<select class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" name="faculty_id">
<option value="selected:" selected></option>
@foreach($instructors as $emp)
<option value="{{ $emp->user_id }}">{{ $emp->firstname.' '.$emp->lastname }}</option>
@endforeach
</select>
如您所见,我正在尝试使用要插入到 faculty_id 列中的 user_id 值。我做错了吗?
我确实将 $instructors 作为全局数组放入 AppServiceProviders
控制器代码
class JoinTable extends Component
{
use WithPagination;
public $faculty_id,$firstname,$lastname,$fullnames,$emp_id,$emp,$appointmentid,$corsno,$corsdes,$day,$c_time,$hours,$room,$section,$size,$sem,$sy,$unit,$userid,$user_id;
public $openEdit=0;
public function render()
{
$dupeSchedules = DB::table('load_schedule')
->select('room', 'hours', 'day','sy','sem')
->groupBy('room', 'hours', 'day','sy','sem')
->havingRaw('COUNT(*) > 1');
//Sub-Query for isolating duplicated schedules//
$dupliSchedules=DB::table('load_schedule')
->join('emp_info', 'load_schedule.faculty_id', '=','emp_info.user_id')
->select('load_schedule.*', 'emp_info.firstname', 'emp_info.lastname')
->joinSub($dupeSchedules, 'dupe_schedules', function ($join) {
$join->on('load_schedule.room','=', 'dupe_Schedules.room');
$join->on('load_schedule.day','=', 'dupe_Schedules.day');
$join->on('load_schedule.hours','=', 'dupe_Schedules.hours');
$join->on('load_schedule.sy','=', 'dupe_Schedules.sy');
$join->on('load_schedule.sem','=', 'dupe_Schedules.sem');
})->paginate(10);
return view('livewire.join-table',compact('dupliSchedules'));
}
public function edit($id)
{
$emp=Emp_sched::findorFail($id);
$this->emp_id=$emp->id;
$this->faculty_id=$emp->faculty_id;
$this->corsno=$emp->corsno;
$this->corsdes=$emp->corsdes;
$this->day=$emp->day;
$this->c_time=$emp->c_time;
$this->hours=$emp->hours;
$this->room=$emp->room;
$this->section=$emp->section;
$this->size=$emp->size;
$this->sem=$emp->sem;
$this->sy=$emp->sy;
$this->unit=$emp->unit;
$this->user_id=$emp->user_id;
$this->editModal();
}
public function editModal(){
$this->openEdit=true;
}
public function closeModal(){
$this->openEdit=false;
}
public function store(){
$this->validate([
'corsdes'=>'required',
'c_time'=>'required'
]);
Emp_sched::updateOrCreate(['id'=>$this->emp_id],[
'user_id'=>$this->user_id,
'faculty_id'=>$this->faculty_id,
'corsno'=>$this->corsno,
'corsdes'=>$this->corsdes,
'day'=>$this->day,
'c_time'=>$this->c_time,
'hours'=>$this->hours,
'room'=>$this->room,
'section'=>$this->section,
'size'=>$this->size,
'sem'=>$this->sem,
'sy'=>$this->sy,
'unit'=>$this->unit
]);
session()->flash('message',
$this->emp_id ? 'Updated Successfully.' : 'Created Successfully.');
$this->closeModal();
$this->resetCreateForm();
}
public function create(){
$this->editModal();
}
public function delete($id){
Emp_sched::findorFail($id)->delete();
session()->flash('warning', 'Deleted Successfully.');
$this->closeModal();
}
private function resetCreateForm()
{
$this->user_id='';
$this->faculty_id= '';
$this->corsno= '';
$this->corsdes= '';
$this->day= '';
$this->c_time= '';
$this->hours= '';
$this->room= '';
$this->section= '';
$this->size= '';
$this->sem= '';
$this->sy= '';
$this->unit= '';
$this->userid= '';
}
}
***************************************************
还有另一种解决方案,您没有将控制器和模型发布到此处作为示例。
protected $fillable = [
'faculty_id', 'corsname', 'day', 'room', 'time'
];
以上代码将在您的模型中。如果你的控制器代码没问题
你select应该是这样的
<select class="" name="faculty_id" id="faculty_id" >
这是一个例子
<select class="form-control" name="roles" id="roles" required="">
<option value="0" selected="" disabled="">Select Role</option>
@foreach ($roles as $role)
<option value="{{$role->id}}" @if($roleid==$role->id) selected @endif>{{$role->name}}</option>
@endforeach
</select>
如果你的控制器和模型都不错,试试下面的代码
<option name ="faculty_id" value="{{ $emp->user_id }}">{{ $emp->firstname.' '.$emp->lastname }}</option>
我在这里添加某人的建议以帮助人们,如果您在 Livewire 组件中,select 元素必须绑定到后端 属性。
<select class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" wire:model="faculty_id">
<option value=""></option>
@foreach($instructors as $emp)
<option value="{{ $emp->user_id }}">{{ $emp->firstname.' '.$emp->lastname }}</option>
@endforeach
</select>
然后在 livewire 组件中
public $faculty_id;
...///...
public function updatedFacultyId($value)
{
dd($value); // on select change, this line must be dumped
}
检查 Ronny 回答的 class 模型中的步骤后,如果您在 Livewire 组件中,select 元素必须绑定到后端 属性.
<select class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" wire:model="faculty_id">
<option value=""></option>
@foreach($instructors as $emp)
<option value="{{ $emp->user_id }}">{{ $emp->firstname.' '.$emp->lastname }}</option>
@endforeach
</select>
然后在 livewire 组件中
public $faculty_id;
...///...
public function updatedFacultyId($value)
{
dd($value); // on select change, this line must be dumped
}
我的数据库中有两个表 "emp_info" 包含(user_id、名字、姓氏) 和 "load_schedule" contains(faculty_id, corsname, day, room, time). 问题是我有一个编辑模式(弹出窗口),它包含在我的主视图中,尽管它显示了讲师列表 ($instructors) 但它无法更新 faculty_id 具有新选定值的列。
到目前为止试过这个:
<select class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" name="faculty_id">
<option value="selected:" selected></option>
@foreach($instructors as $emp)
<option value="{{ $emp->user_id }}">{{ $emp->firstname.' '.$emp->lastname }}</option>
@endforeach
</select>
如您所见,我正在尝试使用要插入到 faculty_id 列中的 user_id 值。我做错了吗? 我确实将 $instructors 作为全局数组放入 AppServiceProviders
控制器代码
class JoinTable extends Component
{
use WithPagination;
public $faculty_id,$firstname,$lastname,$fullnames,$emp_id,$emp,$appointmentid,$corsno,$corsdes,$day,$c_time,$hours,$room,$section,$size,$sem,$sy,$unit,$userid,$user_id;
public $openEdit=0;
public function render()
{
$dupeSchedules = DB::table('load_schedule')
->select('room', 'hours', 'day','sy','sem')
->groupBy('room', 'hours', 'day','sy','sem')
->havingRaw('COUNT(*) > 1');
//Sub-Query for isolating duplicated schedules//
$dupliSchedules=DB::table('load_schedule')
->join('emp_info', 'load_schedule.faculty_id', '=','emp_info.user_id')
->select('load_schedule.*', 'emp_info.firstname', 'emp_info.lastname')
->joinSub($dupeSchedules, 'dupe_schedules', function ($join) {
$join->on('load_schedule.room','=', 'dupe_Schedules.room');
$join->on('load_schedule.day','=', 'dupe_Schedules.day');
$join->on('load_schedule.hours','=', 'dupe_Schedules.hours');
$join->on('load_schedule.sy','=', 'dupe_Schedules.sy');
$join->on('load_schedule.sem','=', 'dupe_Schedules.sem');
})->paginate(10);
return view('livewire.join-table',compact('dupliSchedules'));
}
public function edit($id)
{
$emp=Emp_sched::findorFail($id);
$this->emp_id=$emp->id;
$this->faculty_id=$emp->faculty_id;
$this->corsno=$emp->corsno;
$this->corsdes=$emp->corsdes;
$this->day=$emp->day;
$this->c_time=$emp->c_time;
$this->hours=$emp->hours;
$this->room=$emp->room;
$this->section=$emp->section;
$this->size=$emp->size;
$this->sem=$emp->sem;
$this->sy=$emp->sy;
$this->unit=$emp->unit;
$this->user_id=$emp->user_id;
$this->editModal();
}
public function editModal(){
$this->openEdit=true;
}
public function closeModal(){
$this->openEdit=false;
}
public function store(){
$this->validate([
'corsdes'=>'required',
'c_time'=>'required'
]);
Emp_sched::updateOrCreate(['id'=>$this->emp_id],[
'user_id'=>$this->user_id,
'faculty_id'=>$this->faculty_id,
'corsno'=>$this->corsno,
'corsdes'=>$this->corsdes,
'day'=>$this->day,
'c_time'=>$this->c_time,
'hours'=>$this->hours,
'room'=>$this->room,
'section'=>$this->section,
'size'=>$this->size,
'sem'=>$this->sem,
'sy'=>$this->sy,
'unit'=>$this->unit
]);
session()->flash('message',
$this->emp_id ? 'Updated Successfully.' : 'Created Successfully.');
$this->closeModal();
$this->resetCreateForm();
}
public function create(){
$this->editModal();
}
public function delete($id){
Emp_sched::findorFail($id)->delete();
session()->flash('warning', 'Deleted Successfully.');
$this->closeModal();
}
private function resetCreateForm()
{
$this->user_id='';
$this->faculty_id= '';
$this->corsno= '';
$this->corsdes= '';
$this->day= '';
$this->c_time= '';
$this->hours= '';
$this->room= '';
$this->section= '';
$this->size= '';
$this->sem= '';
$this->sy= '';
$this->unit= '';
$this->userid= '';
}
}
***************************************************
还有另一种解决方案,您没有将控制器和模型发布到此处作为示例。
protected $fillable = [
'faculty_id', 'corsname', 'day', 'room', 'time'
];
以上代码将在您的模型中。如果你的控制器代码没问题 你select应该是这样的
<select class="" name="faculty_id" id="faculty_id" >
这是一个例子
<select class="form-control" name="roles" id="roles" required="">
<option value="0" selected="" disabled="">Select Role</option>
@foreach ($roles as $role)
<option value="{{$role->id}}" @if($roleid==$role->id) selected @endif>{{$role->name}}</option>
@endforeach
</select>
如果你的控制器和模型都不错,试试下面的代码
<option name ="faculty_id" value="{{ $emp->user_id }}">{{ $emp->firstname.' '.$emp->lastname }}</option>
我在这里添加某人的建议以帮助人们,如果您在 Livewire 组件中,select 元素必须绑定到后端 属性。
<select class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" wire:model="faculty_id">
<option value=""></option>
@foreach($instructors as $emp)
<option value="{{ $emp->user_id }}">{{ $emp->firstname.' '.$emp->lastname }}</option>
@endforeach
</select>
然后在 livewire 组件中
public $faculty_id;
...///...
public function updatedFacultyId($value)
{
dd($value); // on select change, this line must be dumped
}
检查 Ronny 回答的 class 模型中的步骤后,如果您在 Livewire 组件中,select 元素必须绑定到后端 属性.
<select class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" wire:model="faculty_id">
<option value=""></option>
@foreach($instructors as $emp)
<option value="{{ $emp->user_id }}">{{ $emp->firstname.' '.$emp->lastname }}</option>
@endforeach
</select>
然后在 livewire 组件中
public $faculty_id;
...///...
public function updatedFacultyId($value)
{
dd($value); // on select change, this line must be dumped
}