skip to Main Content

I’m trying to create a patient card which is automatically added to the "seated waiting" zone at the beginning it showed me the card and in a fraction of a second it disappeared
how can I display the card permanently?
when I asked ChatGPT he told me that it’s a redirection problem but I don’t see where this problem is

here is my controller

public function storePatient(Request $request)
{
    $validatedData = $request->validate([
        'nom' => 'required|string|max:255',
        'nomJeuneFille' => 'nullable|string|max:255',
        'lieuNaissance' => 'required|string|max:255',
        'telephone' => 'required|string|max:255',
        'email' => 'nullable|email|max:255',
        'prenom' => 'required|string|max:255',
        'dateNaissance' => 'required|date',
        'adresse' => 'required|string|max:255',
        'numeroAssurance' => 'required|string|max:255',
        'sexe' => 'required',
    ]);

    $patient = Patient::create($validatedData);

    $dateNaissance = new DateTime($patient->dateNaissance);
    $aujourdhui = new DateTime();
    $diff = $dateNaissance->diff($aujourdhui);
    $age = $diff->y;

    $icone = ($patient->sexe === 'homme') ? 'male.png' : 'femelle.png';

    $cartePatient = "<div class='carte-patient'>
                        <p>Nom: {$patient->nom} {$patient->prenom}</p>
                        <p>Âge: {$age}</p>
                        <img src='images/{$icone}' alt='Genre'>
                    </div>";

    $cartesPatients = session()->has('cartesPatients') ? session('cartesPatients') : [];
    $cartesPatients[] = $cartePatient;


    return back()->with(['success' => 'Patient ajouté avec succès.', 'cartesPatients' => $cartesPatients]);
}

my routes:

<?php

use AppHttpControllersAdminController;
use AppHttpControllersChangerMotDePasseController;
use AppHttpControllersLoginController;
use AppHttpControllersSallesController;
use IlluminateSupportFacadesRoute;

Route::get('/', [LoginController::class,'login'])->name('Login.login');
Route::post('/', [LoginController::class,'loginPost']);
Route::get('/logout', [LoginController::class,'logout'])->name('logout');

Route::middleware(['auth'])->group(function () {
    Route::get('/administrateur', [AdminController::class,'index'])->name('Admin.index');
    Route::post('/administrateur', [AdminController::class,'store']);
    Route::post('/unites-fonctionnelles', [AdminController::class, 'storeUniteFonctionnelle'])->name('unites-fonctionnellesStore');;
    Route::get('/changer_mot_de_passe', [ChangerMotDePasseController::class,'index'])->name('ChangerMdp.index');
    Route::post('/changer-mot-de-passe', [ChangerMotDePasseController::class, 'update'])->name('postChangerMotDePasse');
    Route::get('/salles', [SallesController::class,'index'])->name('Salles.index');
    Route::get('/medecins', [AdminController::class,'getMedecins'])->name('Admin.getMedecins');
    Route::get('/infirmiers', [AdminController::class,'getInfirmiers'])->name('Admin.getInfirmiers');
    Route::get('/admin', [AdminController::class,'getAdmins'])->name('Admin.getAdmins');
    Route::get('/logoutP', [SallesController::class,'logoutP'])->name('logoutP');
    Route::get('/logout', [SallesController::class,'logout'])->name('logout');
    Route::get('/options', [SallesController::class, 'getOptions']);
    Route::post('/ajouter-patient', [SallesController::class, 'storePatient'])->name('AjouterPatient');
});

and this is where the card is supposed to be:

<div class="zone_attente">
                        <p class="titre">Zone d'attente</p>
                        <div class="attente_assise" id="attenteAssise">
                            <p class="titre_secondaire">Attente assise</p>
                            @if(session()->has('cartePatient'))
                            <div class="attenteAssise">
                                {!! session('cartePatient') !!}
                            </div>
                            @endif
                        </div>
                        <div class="attente_couchee">
                            <p class="titre_secondaire">Attente couchée</p>
                            <div class="attenteCouchee"></div>
                        </div>
                    </div>

2

Answers


  1. It seems like there’s a mismatch in the session key names between your controller and your view.

    In your controller, you’re storing the patient card in the session variable named cartesPatients, but in your view, you’re checking for cartePatient. This difference in session key names causes the data to be briefly displayed.

    To fix this issue, ensure that you’re using the correct session key name in both places:

    Controller:

    $cartesPatients = session()->has('cartesPatients') ? session('cartesPatients') : [];
    $cartesPatients[] = $cartePatient;
    
    return back()->with(['success' => 'Patient ajouté avec succès.', 'cartesPatients' => $cartesPatients]);
    

    View:

    <div class="zone_attente">
        <p class="titre">Zone d'attente</p>
        <div class="attente_assise" id="attenteAssise">
            <p class="titre_secondaire">Attente assise</p>
            @if(session()->has('cartesPatients'))
                <div class="attenteAssise">
                    @foreach(session('cartesPatients') as $cartePatient)
                        {!! $cartePatient !!}
                    @endforeach
                </div>
            @endif
        </div>
        <div class="attente_couchee">
            <p class="titre_secondaire">Attente couchée</p>
            <div class="attenteCouchee"></div>
        </div>
    </div>
    
    Login or Signup to reply.
  2. try this

    public function storePatient(Request $request)
    {
        // Your validation code...
    
        $patient = Patient::create($validatedData);
    
        // Build the patient card HTML
        $dateNaissance = new DateTime($patient->dateNaissance);
        $aujourdhui = new DateTime();
        $diff = $dateNaissance->diff($aujourdhui);
        $age = $diff->y;
        $icone = ($patient->sexe === 'homme') ? 'male.png' : 'femelle.png';
        $cartePatient = "<div class='carte-patient'>
                            <p>Nom: {$patient->nom} {$patient->prenom}</p>
                            <p>Âge: {$age}</p>
                            <img src='images/{$icone}' alt='Genre'>
                        </div>";
    
        // Store the patient card in the session
        $cartesPatients = session()->has('cartesPatients') ? session('cartesPatients') : [];
        $cartesPatients[] = $cartePatient;
        session(['cartesPatients' => $cartesPatients]);
    
        return back()->with(['success' => 'Patient ajouté avec succès.']);
    }
    

    for view

    public function storePatient(Request $request)
    {
        // Your validation code...
    
        $patient = Patient::create($validatedData);
    
        // Build the patient card HTML
        $dateNaissance = new DateTime($patient->dateNaissance);
        $aujourdhui = new DateTime();
        $diff = $dateNaissance->diff($aujourdhui);
        $age = $diff->y;
        $icone = ($patient->sexe === 'homme') ? 'male.png' : 'femelle.png';
        $cartePatient = "<div class='carte-patient'>
                            <p>Nom: {$patient->nom} {$patient->prenom}</p>
                            <p>Âge: {$age}</p>
                            <img src='images/{$icone}' alt='Genre'>
                        </div>";
    
        // Store the patient card in the session
        $cartesPatients = session()->has('cartesPatients') ? session('cartesPatients') : [];
        $cartesPatients[] = $cartePatient;
        session(['cartesPatients' => $cartesPatients]);
    
        return back()->with(['success' => 'Patient ajouté avec succès.']);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search