dimanche 30 avril 2023

Mark updated checkbox of a many-to-many relationship in laravel

I am trying to show the updated checkboxes that are fetched from a many-to-many relationship.

Model Candidato:

public function estadox()
    {    
        return $this->hasMany(CandidatoEstadoPostulacion::class, 'status_id', 'user_id');
    }

Model Estado Postulacion

public function estados()
    {
        return $this->hasMany(CandidatoEstadoPostulacion::class, 'status_id', 'id');
    }

Model CandidatoEstadoPostulacion

    public function candidatos()
    {
        return $this->hasMany(Candidato::class,  'status_id', 'user_id');
    }

    public function estados()
    {
        return $this->hasMany(EstadoPostulacion::class, 'id', 'status_id');
    }

Controller Candidatos

public function index( Vacancy $vacante, Candidato $candidato )
    {
        $estados = EstadoPostulacion::pluck('status', 'id');

        $ca = Candidato::with('estadox')->get();

        return view('candidatos.index', compact('vacante', 'estados', 'candidato') );
    }

table Candidatos :

| id | user_id| vacancy_id|

table estado_postulaciones :

| id | status |

table candidato_estado_postulaciones : | status_id | user_id |

view candidatos/index.blade.php

 @forelse ($estados as $id => $estado)
     <div>
          <input type="checkbox" name="status[]" id="-" class="peer hidden" value=""  onchange="this.form.submit()">
          <label for="-" class="block cursor-pointer select-none rounded-xl p-2 text-center peer-checked:bg-blue-500 peer-checked:font-bold peer-checked:text-white">
          <i class="fas fa-folder fa-2x"></i>
          <div class="w-full text-xs">Está en:</div>
          <div class="w-full text-xs text-gray-800 font-semibold uppercase"></div>
          </label>
   </div>
   @empty
      <li>No existe ningun estado en la BD</li>
   @endforelse

Image of what I need it to show: enter image description here




Aucun commentaire:

Enregistrer un commentaire