i have an empty result in commune2 when i execute this query in laravel controller

Viewed 31

php controller:

As you can see, i wan to extract the value of $commune2 normally it get one or null value. i try to change a value of $commune2 by a constant so the result is good, but if i use it like a result of a query it showing empty

class DspController extends Controller
{
   function afficher(Request $req){
       $idwi = session('wilaya');
       
        if ($req->ajax()) {
            $decision = $req->decision;
            if(session('role') == '2'){ // Les demandes de la wilaya
                $data = DB::table('choixes')
                    ->join('specialistes', 'choixes.idSpecialiste', '=', 'specialistes.id')
                    ->join('demandes AS dmd1', 'dmd1.id', '=', 'choixes.idDemande')
                    ->join('communes AS com1', function($join){
                        $join->on('choixes.idWilaya','=', 'com1.code_w');
                        $join->on('choixes.idCommune', '=', 'com1.code_com');
                    })
                    ->join('specialites', 'dmd1.laSpecialite', 'specialites.id')
                    ->select('specialistes.nom', 'specialistes.prenom', 'dmd1.type'
                             , 'choixes.decision', 'com1.commune'
                             , 'choixes.id AS dId', 'dmd1.id AS cId'
                             , 'specialites.specialite')
                    ->where('choixes.idWilaya', '=', $idwi)
                    ->where('choixes.decision', '=', $decision)
                    ->where('choixes.order', '=', 1)
                    ->orderBy('choixes.created_at', 'ASC')
                    ->orderBy('choixes.order')
                    ->groupBy('choixes.id')
                    ->get();                

                return Datatables::of($data) 
                ->addIndexColumn()

                ->addColumn('commune2', function($row){
                    $idwi = session('wilaya');

                    $commune2 = DB::table('communes AS com2')
                        ->join('choixes AS ch2', function($join)
                            {
                                $join->on('ch2.idCommune', '=', 'com2.code_com')
                                    ->where('ch2.idWilaya', '=', 'com2.code_w');
                            })
                        ->join('demandes AS dmd2', 'dmd2.id', '=', 'ch2.idDemande')
                        ->select('com2.commune')
                        ->where('com2.code_w', '=', $idwi)
                        ->where('ch2.idDemande', '=', $row->cId)
                        ->where('ch2.order', '=', 2)
                        ->get()->first();

                          return $commune2;
                })
                ->addColumn('detail', function($row){
                        $actionBtn1 = '<a class="detail btn btn-success btn-sm" id="D'.$row->dId.'" onClick="detailD(this.id)">Détail</a>';
                        return $actionBtn1;
                    })
                ->addColumn('repondre', function($row){
                        $actionBtn2 = '<a class="repondre btn btn-warning btn-sm" id="R'.$row->dId.'" onClick="affiche(this.id)">Repondre</a>';
                        return $actionBtn2;
                    })
                ->rawColumns(['commune2','detail','repondre'])
                ->make(true);                    
            }
        }
    }

}

Javascript & Ajax: Receive DATA from DspController.php and diplay it in dspList.blade.php

var decision = $("input[name='dmdDecider']:checked").val();

         function listeDemandes(decision){
             
            var role = {{ session('role') }}
            var columns = [
                {data: 'nom', name: 'nom', orderable: true, searchable: true},
                {data: 'prenom', name: 'prenom'},
                {data: 'type', name: 'demande'},
                {data: 'specialite', name: 'specialite'},
                {data: 'commune', name: 'commune'},
                {data: 'commune2', name: 'commune2'},
                {data: 'decision', name: 'dob', render: function(data, type, row){
                    sev='';
                    switch (data){
                        case '0':
                        sev ='<span class="badge badge-dark badge-pill">Non Traitée</span>';
                        break;
                        case '1':
                        sev ='<span class="badge badge-success badge-pill">Acceptée</span>';
                        break;
                        case '2':
                        sev ='<span class="badge badge-danger badge-pill">Refusée</span>';
                        break;
                    }
                     return sev;
                }, orderable: true, searchable: true
                },
                {data: 'detail', name: 'detail', orderable: false, searchable: false},
            ]
            if(role == 2){
                columns.push({data: 'repondre', name: 'repondre', orderable: false, searchable: false});
            }
            
            
           $('#tabListe').DataTable({
                processing: true,
                serverSide: true,
                ajax: { url: "{{ route('dspListe.liste') }}",
                        data: {decision: decision}
                      }, 
                columns: columns
            });            
            
         }

    $(document).ready(function(){
    
        labelInput = 'lab'+$('input[type=radio][name=dmdDecider]:checked').attr('id');
             decision = $("input[name='dmdDecider']:checked").val();
             $('#tabListe').DataTable().destroy();
             listeDemandes(decision);
     
    });

HTML:

    <div class="row">
         <div id="formulaireDmd" class="form my-5 mx-4">
              <div class="row" style="border-bottom: 2px solid rgba(252, 243, 243, 0.3);">
                  <div class="table-responsive px-2">
                      <table class="table table-hover table-striped" id="tabListe">
                          <thead>
                            <tr class="text-center">
                                <th>Nom</th>
                                <th>Pr&eacute;nom</th>
                                <th>Demande de</th>
                                <th>Sp&eacute;cialit&eacute;</th>
                                <th>Commune Choix 1</th>
                                <th>Commune Choix 2</th>
                                <th id="trait">Traitement</th>
                                <th>Afficher</th>
                             @if(session('role') == '2')
                                <th>R&eacute;pondre</th>
                             @endif
                            </tr>
                          </thead>    
    
                     </table>
                </div>
           </div>
        </div>
   </div>
0 Answers
Related