laravel模态显示客户端信息

laravel modal show Client informations

我正在获取客户信息:

<table class="mytable" id="table">
<tr><th>Nom</th><th>Prenom</th><th>CIN</th><th>Email</th><th 
width="20%">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>

当我点击显示按钮时,我想显示客户的信息。 目前,这段代码一切正常:

$(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'));
});

我用这个模态显示它:

<div class="table-responsive"> 
<table class="table table-striped table-bordered">
<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>
<h4 style="color:#005b7f">Liste des voitures occupé par ce client</h4>
<table class="mytable">

<tr><th>Date de prise</th><th>Date de fin</th><th>Matricule</th><th>details 
de contrat</th></tr>
@foreach ($client->locations as $location)
<tr><td>{{$location->date_prise}}</td><td>{{$location->date_fin}}</td><td> 
{{$location->voiture_matricule}}</td><td><a href="#">Voir plus</a></td></tr>
@endforeach 
</table>  

问题出在这里: 我在 table 客户端和 table 位置之间有关系。 table voiture 也与 table 位置有关。 这是客户端模式:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class client extends Model
{
protected $primaryKey = 'cin';

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

当我点击显示按钮时,我想在显示客户信息的同时,显示该客户占用的汽车。 然后我需要将我客户的 cin 传递给我的控制器或我的视图。知道如何做到这一点吗?

首先,在 header

的部分添加以下条目
<meta name="csrf-token" content="{{ csrf_token() }}" />

您可以在点击事件上调用 ajax 函数

$(document).on('click', '.show-modal', function() {
    //Along with your modal action you can use following code
     $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    var id = $(this).data('nom');
    $.post('client/'+id, 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');
});

在这里,url 应该匹配您为客户声明的路由的 url,例如,如果您声明了这样的路由:

Route::post('client/{id}', 'Controller@getclient');

您可以使用以下函数获取客户信息:

public function getClientData($id){
    $car_data = array();
    //with client id you can write logic and send output in car_data variable  through json
    return response()->json(['success' => true, 'car_data' => $car_data]);
}

您可以删除模式中 h4 标签下方的 foreach 循环 html 并使用以下代码

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

希望这会给你一些想法。

感谢您的回复,它很有帮助。 我认为你的逻辑是对的。我也这样做了,但我仍然一无所获

这是我的 ajax :

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

//show function 
 $(document).on('click', '.show-modal', function() {
//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');

});

这是我的控制器,我测试了我的控制器的输出并且它正在工作,所以我认为问题出在 ajax 函数中:

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]);
}

我的路线:

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

我认为问题可能出在 ajax 函数中,因为我什至没有得到

var html = '<tr><th>Date de prise</th><th>Date de fin</th><th>Matricule</th> 
<th>details de contrat</th></tr>' ;

在我看来。

我还在 header 中添加了元标记。 谢谢你的回答。