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

ayoub's avatar
Level 1

laravel show Clients informations in Modal with ajax

i am developing a car rental Application . i have three tables: Clients , Cars and locations.

the table locations : have the cin of the Client as foreign key , and also the Car id as foreign key.

here is the relations between client and location modal:

class client extends Model
{

protected $primaryKey = 'cin';

public $incrementing = false;

public function locations()
{
    return $this->hasMany(Location::class);
}

}

here i display all clients :

   <table class="mytable" id="table">
  <tr><th>Nom</th><th>Prenom</th><th>CIN</th><th>Email</th><th >Action</th></tr>
  {{ csrf_field() }}

  @foreach($clients as $client)
  <tr class="client{{$client->cin}}">
  <td>{{ $client->nom }}</td><td>{{ $client->prenom }}</td><td>{{ $client- 
  >cin }}</td><td>{{ $client->email }}</td>
 <td>
 <span><a type="button" class="show-modal" href="#"  data-nom="{{$client- 
 >nom}}" data-prenom="{{$client->prenom}}"  data-tel="{{$client->tel}}" 
 data-cin="{{$client->cin}}" data-email="{{$client->email}}"><img 
 src="img/ac3.png"></a></span>
 </td></tr>
 @endforeach
 </table>

when i click on a client : i want to show his informations and also the cars that he rents .

i am able to show his informations, but i am not able to show the cars he rents.

here is the modal:

<div id="show" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
       <h4 class="modal-title" style="color:#005b7f;"> Client Infos</h4> 
    </div> 
        
    <div class="modal-body">                     
       <div id="modal-loader" style="display: none; text-align: center;">
       <!-- ajax loader -->
       <img src="img/ajax-loader.gif">
       </div>
       
 <tr>
    <th >NOM</th>
 <td id="nom"></td>
 </tr>
<tr>
 <th >PRENOM</th>
<td id="prenom"></td>
</tr>
 <th >TELEPHONE</th>
 <td id="tel"></td>
 </tr>
 <th >CIN</th>
 <td id="cin"></td>
     </tr>
 <th >Email</th>
 <td id="email"></td>
 </tr>

       </table>

      <br><br>

           <h4 style="color:#005b7f">Liste des voitures occupé par ce client</h4>

//the rent car should be showed here

           <table class="mytable" id="mytable">
            
          </table>               

     </div>

      </div> 
                    
    <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
    </div> 
                    
     </div> 
 </div>
</div>

My Ajax Function:

    $.ajaxSetup({
       headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
   });

//show function 
  $(document).on('click', '.show-modal', function() {
  $('#show').modal('show');
  $('#nom').text($(this).data('nom'));
  $('#prenom').text($(this).data('prenom'));
  $('#tel').text($(this).data('tel'));
  $('#cin').text($(this).data('cin'));
 $('#email').text($(this).data('email'));


 //Along with your modal action you can use following code

 var cin = $(this).data('cin');
 $.post('client/'+cin, function(response){ 
    if(response.success)
    {
        var html = '<tr><th>Date de prise</th><th>Date de fin</th><th>Matricule</th>         
    <th>details de contrat</th></tr>' ;
        $.each(response.car_data, function(i, car_data){
            html = html + "<td>" + car_data.date_prise +"</td>" + "<td>" + car_data.date_fin +"      
   </td>"
            +"<td>" + car_data.voiture_matricule +"</td>";
        }).appendTo("#mytable");
    }

    },'json');

  });

my Route:

        Route::post('client/{cin}', 'ClientsController@getclientData');

my Controller

    public function getclientData($cin){
 $car_dat = array();
 //with client id you can write logic and send output in car_data variable  through json
 $car_dat= Client::find($cin);
 $car_data=array();
 $car_data=$car_dat->locations;

  return response()->json(['success' => true, 'car_data' => $car_data]);
}

the logic in the controller works well, so i think the problem is in the Ajax function.

Problem description: when i click the button show i pass the id of the client to my controller,then i want to show Client rent car with ajax , but nothing shown ..

0 likes
3 replies
zion's avatar

Could you please format the code properly first, include a problem description aswel as there seem to be none.

1 like
ayoub's avatar
Level 1

is there any problem in my ajax function ?

Please or to participate in this conversation.