Search code examples
laravellaravel-livewire

Laravel 8 Livewire Save ID of Selected Value in Drop Down


I have two tables in my database "emp_info" contains(user_id, firstname, lastname) and "load_schedule" contains(faculty_id, corsname, day, room, time). The problem is I have an edit modal(pop-up) which is included in my main view although it displays the list of instructors ($instructors) but it can't update the faculty_id column with the new selected value.

Tried this so far:

<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>

As you can see I am trying to use the user_id value to be inserted in the faculty_id column. Am I doing it wrong? I did put $instructors as global array in AppServiceProviders


Controller Codes

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= '';
    }


}  
***************************************************

Solution

  • There is an another solution, You didn't posted your controller and model to here is example.

    protected $fillable = [
    
            'faculty_id', 'corsname', 'day', 'room', 'time'
        ];

    Above code will be in you model. If your controller codes are fine You select should be like this.

    <select class="" name="faculty_id" id="faculty_id" >
    

    Here is an example

     <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>

    Try this bellow code If your controller and model are good

    <option name ="faculty_id" value="{{ $emp->user_id }}">{{ $emp->firstname.' '.$emp->lastname }}</option>
            
        
    

    I adding here someones suggestion to help people, if you are in a Livewire component the select element must be bind to the backend property.

    <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>
    

    then in the livewire component

    public $faculty_id;
    
    ...///...
    
    public function updatedFacultyId($value)
    {
        dd($value); // on select change, this line must be dumped
    }