【问题标题】:Json response with relationship in laravel bladeJson响应与laravel刀片中的关系
【发布时间】:2019-02-09 01:40:09
【问题描述】:

如何使用 ajax 获取 $levels 并将其显示在表格中.. 注意:$levels 有关系..

控制器:

    public function days(Request $request){

    $levels=Level::with('lead')->get();

    $days=array();
    $id = $request->input('months');
    $month = $id;
    $year = 2018;
    for($d=1; $d<=31; $d++) 
    {
        $time=mktime(12, 0, 0, $month, $d, $year);  
        if(date('m', $time)==$month && date('w', $time)>0 && date('w', $time)<6) 
        {
            $days[]=date('Y-m-d H:i:s', $time);
        } 
    }
    return response()->json($days);
   }

阿贾克斯:

$(document).ready(function(){
$('.testing').on('change',function(){
    var test=$(this).val(); 
    $.ajax({
     type:'get',
     url:'/days',
     data:{"months":test,"_token": "{{ csrf_token() }}"},
     success: function(data){
            $("#days").empty();
            $.each(data,function(index,value){
                $("#days").append('<input type="checkbox" checked 
           value="'+value+'" name="days[]"  > '+value+'<br/>');
            });
          }
      });
   }); 
});

.................................................. ..................................................... ..................................................... ......

【问题讨论】:

  • 试试这个return response()-&gt;json(['level' =&gt; $levels], 200);
  • 我的 $days 会怎样?
  • 你可以返回任何你想要的,我告诉你使用相同的语法。
  • 返回在复选框内,如何从复选框中删除它?
  • 尝试使用 console.log(data) 来控制您收到的数据。然后在这里发布结果。

标签: php jquery laravel laravel-blade


【解决方案1】:

你应该使用像return response()-&gt;json(['days' =&gt; $days, 'levels' =&gt; $levels]);这样的数组来压缩

控制器

public function days(Request $request){
    $levels=Level::with('lead')->get();

    $days=array();
    $id = $request->input('months');
    $month = $id;
    $year = 2018;
    for($d=1; $d<=31; $d++) {
        $time=mktime(12, 0, 0, $month, $d, $year);  
        if(date('m', $time)==$month && date('w', $time)>0 && date('w', $time)<6) {
            $days[]=date('Y-m-d H:i:s', $time);
        } 
    }
    return response()->json(['days' => $days, 'levels' => $levels]);
}

HTML 示例

<table class="table"></table>

JS 试试这样:

$(document).ready(function(){
    $('.testing').on('change',function(){
        var test=$(this).val(); 
        $.ajax({
         type:'get',
         url:'/days',
         data:{"months":test,"_token": "{{ csrf_token() }}"},
         success: function(data){
                $("#days").empty();
                $.each(data['days'],function(index,value){
                    $("#days").append('<input type="checkbox" checked value="'+value+'" name="days[]"  > '+value+'<br/>');
                });

                $.each(data['levels'],function(key,level){
                    $("table.table").append(
                        '<tr>'+
                            '<td>'+level.id+'</td>'+
                            '<td>'+level.name+'</td>'+
                        '</tr>'
                    );
                   // added
                   // if you wish to put all together in one table its up you.
                   if(level.lead){
                        $.each(level.lead,function(leadKey,leadValue){
                            $("table2.table2").append(
                                '<tr>'+
                                    '<td>'+leadValue.id+'</td>'+
                                    '<td>'+leadValue.name+'</td>'+
                                '</tr>'
                            );
                        });
                    }
                     //
                });
            }
        });
    });
});

【讨论】:

  • 线索不是从关系中出来的
  • level.lead.name
  • 发布您收到的回复数据,我们将很容易为您提供帮助
  • 我更新了我的答案,不要复制粘贴,它只是一个示例。
  • if 语句不起作用我尝试将结果放在不同的表上呵呵
猜你喜欢
  • 1970-01-01
  • 2013-01-06
  • 2016-12-19
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 2019-04-02
  • 1970-01-01
  • 2017-01-05
相关资源
最近更新 更多