Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Snapey's avatar

no sign here of you actually changing any field names....

noblemfd's avatar

@snapey - I am no longer using the jQuery dynamic input fields. Now do I iterate with employee_type_id:

public function create()
{
    $employeetypes   =       HrEmployeeType::all(); 
    $leavetype = new HrLeaveType();
    
    return view('leave.leave_types.create')
            ->with('leavetype', $leavetype);
}

public function store(StoreLeaveTypeRequest $request)
{
    $leavetype = new HrLeaveType();
    $leavetype->leave_type_name             = $request->leave_type_name;
    $leavetype->leave_type_code             = $request->leave_type_code;

   $leavetype->save();
   foreach ($request->employee_type_id as $key => $employee_type_id){
    $insert_array = [
        'employee_type_id'                  => $request->employee_type_id[$key],    
        'weekend_inclusive'                 => $request->weekend_inclusive[$key] ? 1 : 0,
        'holiday_inclusive'                => $request->holiday_inclusive[$key] ? 1 : 0,
        'leave_type_id'                     => $leavetype->id,
    
         HrLeaveTypeDetail::create($insert_array );

      ];
 }

view

      <tbody>
           @foreach($employeetypes as $key => $employeetype)
           <tr>                             
               <td width="30%">  
                 <span>{{$employeetype->employee_type_name}}</span> 
                 <input type="hidden" name="employee_type_id[]" value="{{$employeetype->employee_type_name}}">    
              </td>                                                           
              <td width="15%"><input type="checkbox" name="weekend_inclusive[{{$employeetype->id}}]"  class="form-control weekend_inclusive" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>
              <td width="15%"><input type="checkbox" name="holiday_inclusive[{{$employeetype->id}}]"  class="form-control holiday_inclusive" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>                                
               </tr>
                         @endforeach
          </tbody>

dd($request->all());

gives me

 array:9 [▼
   "_token" => "OobTM8tB5dgLByj0nGpdak10zgLqghsEHc9Wfg9F"
   "leave_type_name" => "Annual Leave"
   "leave_type_code" => "dddsds"
   "description" => "dsdsdsds"
   "employee_type_id" => array:5 [▼
     0 => "Admin"
     1 => "Manager"
     2 => "Drivers"
     3 => "Directors"
     4 => "Secretary"
   ]
   "weekend_inclusive" => array:5 [▼
     1 => "on"
     2 => "on"
     5 => "on"
     6 => "on"
     7 => "on"
   ]
   "holiday_inclusive" => array:4 [▼
     1 => "on"
     2 => "on"
     5 => "on"
     7 => "on"
   ]
 ]

I observed that: name="weekend_inclusive[{{$employeetype->id}}]"

gives 5 rows, but set everything on while name="holiday_inclusive[{{$employeetype->id}}]"

gives 4 rows instead of 5 and set everything on.

Why is everything set on, and why 4 rows instaed of 5

Snapey's avatar

any radios that are off will not be provided in the response.

noblemfd's avatar

@snapey - But still got this error when I tried to submit:

 #message: "Undefined offset: 0"
  #code: 0
  #file: "C:\xampp\htdocs\myapp\app\Http\Controllers\Leave\LeaveTypesController.php"
  #line: 99
  #severity: E_NOTICE
  trace: {▼
    C:\xampp\htdocs\myapp\app\Http\Controllers\Leave\LeaveTypesController.php:99 {▼
      App\Http\Controllers\Leave\LeaveTypesController->store(StoreLeaveTypeRequest $request) …
      ›                 'weekend_inclusive'                 => $request->weekend_inclusive[$key] ? 1 : 0,
      ›                  'holiday_inclusive'                => $request->holiday_inclusive[$key] ? 1 : 0,
Snapey's avatar

because your field names are not structured consistently. You use zero based index in one place and the row id in other places

newbie360's avatar

too many code here, haven't read all

@foreach ($collections as $item)
    @if ($somethings->contains($item->id))
        <label class="btn btn-light active">
            <input type="checkbox" name="var[]" value="{{ $item->id }}" checked autocomplete="off"> {{ $item->name }}
        </label>
    @else
        <label class="btn btn-light">
            <input type="checkbox" name="var[]" value="{{ $item->id }}" autocomplete="off"> {{ $item->name }}
        </label>
    @endif
@endforeach

depends on how you prepare the collections

newbie360's avatar

hmmm let me think how to make an example o.0

since i haven't read all the comment above, does you means this?

ok i was use bootstrap 4, so i make an example for this

https://getbootstrap.com/docs/4.5/components/buttons/#checkbox-and-radio-buttons

<div class="btn-group-toggle" data-toggle="buttons">
  <label class="btn btn-secondary active">
    <input type="checkbox" checked> Checked
  </label>
</div>

you may need to change the css easy to know what the current stats is

<div class="btn-group-toggle my-button-checkbox" data-toggle="buttons">

.my-button-checkbox .active {
  color: white !important;
  background-color: #428bca !important;
  border-color: #428bca !important;
}

a user has many tags

// list all tags one by one
@foreach ($tags as $tag)
    @if ($user->tags->contains($tag->id))
        <label class="btn btn-light active">
            <input type="checkbox" name="tags[]" value="{{ $tag->id }}" checked autocomplete="off"> {{ $tag->name }}
        </label>
    @else
        <label class="btn btn-light">
            <input type="checkbox" name="tags[]" value="{{ $tag->id }}" autocomplete="off"> {{ $tag->name }}
        </label>
    @endif
@endforeach

after submit, now you have a new updated tags[], so you can use it to sync() or etc via the relationships

Snapey's avatar

@noblemfd

You have a limited set of employee types

Loop over them in the view

For each employee type, have a field that is keyed with the employee type id for the two checkboxes.

Then in the controller, again iterate over the same list of employee types and see if the response contains a field with that key.

<tbody>
           @foreach($employeetypes as $employeetype)
           <tr>                             
               <td width="30%">  
                 <span>{{$employeetype->employee_type_name}}</span> 
              </td>                                                           
              <td width="15%"><input type="checkbox" name="types[{{ $employeetype->id }}]weekend_inclusive"  class="form-control weekend_inclusive" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>
              <td width="15%"><input type="checkbox" name="types[{{$employeetype->id}}]holiday_inclusive"  class="form-control holiday_inclusive" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>                                
               </tr>
                         @endforeach
          </tbody>

controller

public function store(StoreLeaveTypeRequest $request)
{
     foreach ($request->types as $id => $type) {

         $leavetype = HrLeaveType::find($id);

         $leavetype->weekend_inclusive  = $type['weekend_inclusive'] ? 1 : 0 ?? 0;
         $leavetype->holiday_inclusive  = $type['holiday_inclusive'] ? 1 : 0 ?? 0;

         $leavetype->save();
    }
}

noblemfd's avatar

Thanks so much @snapey . But from the controller I have two models:

HrLeaveType and HrLeaveTypeDetail

public function store(StoreLeaveTypeRequest $request)
{
    $leavetype = new HrLeaveType();
    $leavetype->leave_type_name             = $request->leave_type_name;
    $leavetype->leave_type_code             = $request->leave_type_code;
    $leavetype->save();

     foreach ($request->employee_type_id as $key => $employee_type_id){
    $insert_array = [
       'employee_type_id'                  => $request->employee_type_id[$key],    
       'weekend_inclusive'                 => $request->weekend_inclusive[$key] ? 1 : 0,
       'holiday_inclusive'                => $request->holiday_inclusive[$key] ? 1 : 0,
       'leave_type_id'                     => $leavetype->id,

        HrLeaveTypeDetail::create($insert_array );

    ];
 }

The arrays goes into HrLeaveTypeDetail and not HrLeaveType

Also there is no way I can use find($id) in HrLeaveTypeDetail

Snapey's avatar

You dont have anything in the detail table?

noblemfd's avatar

@snapey - No. Both HrLeaveType and HrLeaveTypeDetail has nothing. HrEmployeeType has the types of employee and the data there determines the number of rows. employee_type_id in HrLeaveTypeDetail is a foreign key and has its parent in HrLeaveType. This is used to iterate. Since all the data in it will be mapped with other fields and its id are store in HrLeaveTypeDetail

noblemfd's avatar

Thanks so much @snapey . I eventually got it solve.

public function store(StoreLeaveTypeRequest $request)
{
    $leavetype = new HrLeaveType();
    $leavetype->leave_type_name             = $request->leave_type_name;
    $leavetype->leave_type_code             = $request->leave_type_code;
    $leavetype->save();

    foreach ($employeetypes as $key => $employeetype)
    {
        $insert_array = [
            'employee_type_id'                  => $employeetype->id,    
            'weekend_inclusive'                 => isset($request["weekend_inclusive$key"]) ? 1: 0,
            'holiday_inclusive'                    => isset($request["holiday_inclusive$key"]) ? 1: 0,
        ];
        HrLeaveTypeDetail::create($insert_array );
    } 
 }

view

 @foreach ($employeetypes as $key=> $employeetype)
     <td width="10%"><input type="checkbox" name="weekend_inclusive{{$key}}"  class="form-control" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES">
     </td>

Your involvement really got me determined to resolve it.

Thanks

Snapey's avatar

Why, I don't think you followed one thing that I showed ?

Previous

Please or to participate in this conversation.