Search code examples
laravelcheckboxphpwordsetvalue

How do I set Yes or No value in my template PhpWord instead of 1 or 0?


I have a form and a template with format .docx The proccess of replacing variables in the templates is working well except the checkbox. In the value checkbox I have 1 and 0. Now instead of boolean value (0 or 1), i want to have Yes and No. My controller here:

      public function edit(Stagiaire $stagiaire, $downloadName = null)
{
    $id = $stagiaire ->id;
    $desc1 = Stagiaire::find($id);

    $my_template = new \PhpOffice\PhpWord\TemplateProcessor(public_path('templateStagiaire.docx'));

    $my_template->setValue('id', $desc1->id);
    $my_template->setValue('civilite', $desc1->civilite);
    $my_template->setValue('prenoms', $desc1->prenoms);
    $my_template->setValue('nom', $desc1->nom);

    //This 3 inputs are checbox in my form
    $my_template->setValue('regles_deonto', $desc1->regles_deonto);
    $my_template->setValue('reglement_interieur', $desc1->reglement_interieur);
    $my_template->setValue('horaires', $desc1->horaires);
    
    //The name of my file
    $first_name = $stagiaire->prenoms ;
    $last_name = $stagiaire->nom ;
    $filename = $first_name. " ". $last_name;
    

    try{
        $my_template->saveAs(storage_path("$filename.docx"));
    }catch (Exception $e){
        //handle exception
    }
    $downloadName = $downloadName??$filename;

    return response()->download(storage_path("$filename.docx"));
    }

My form

    <div id="container">
                    <input type="checkbox" name="regles_deonto" id="regles_deonto" value="1" required 
    > Régles déontologies
                    <input type="checkbox" name="reglement_interieur" id="reglement_interieur" 
    value="1" required > Réglement Intérieur

                    <input type="checkbox" name="horaires" id="horaires" value="1" required > 
    Horaires
    </div>

My Question is how can I replace the booleans values (0 and 1) by Yes or No in my template Word. I need help please. Thank in advance.


Solution

  • You can just do like this,

    //This 3 inputs are checbox in my form
        $desc1->regles_deonto = (bool)($desc1->regles_deonto) == true ? 'Yes' : 'No'; 
        $my_template->setValue('regles_deonto', $desc1->regles_deonto);
    

    You can do this similarly for other two values.