Home > other >  Laravel 8 Livewire Save ID of Selected Value in Drop Down
Laravel 8 Livewire Save ID of Selected Value in Drop Down

Time:10-16

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

CodePudding user response:

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'
    ];
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Try this bellow code If your controller and model are good

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

CodePudding user response:

after check the steps in the class model of Ronny answer, 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
}
  • Related