如何使用ajax在laravel中应用diffForHumans()?

时间:2016-11-26 06:51:23

标签: ajax laravel

我正在使用laravel和本地ajax。我想知道在使用ajax时我在哪里放置diffForhHumans()。在我的控制器中。我只是返回对象获取。

这是我的控制器

public function getDownlines($id) {
    $upline = Upline::find($id);

    return $upline->downlines;
}

模型

public function downlines() {
    return $this->hasMany('App\Downline');
}

视图中的HTML代码

<div id="downlines">
    <div class="downlines-title-container">
        <p class="title"></p>
    </div>

    <div id="downlines-holder">
        <div class="p_parent_header">
            <p>ID</p>
            <p>Account Code</p>
            <p>Created By</p>
            <p>Created At</p>
        </div>
    </div>
</div>

Ajax中的脚本

var downlines = document.getElementById('downlines'),
    downlines_holder = document.getElementById('downlines-holder');

function getPromise(url) {
    return new Promise(function(resolve, reject) {
        var xhr = new XMLHttpRequest();

        xhr.open('GET', url);

        xhr.onload = function() {
            if(xhr.status == 200) {
                resolve(xhr.response);
            } else {
                reject(Error(xhr.statusText))
            }
        }

        xhr.onerror = function() {
            reject(Error('Network Error'));
        };

        xhr.send();
    })
}

function getDownlines(e, id) {
    getPromise('upline/getdownlines/' + id).then(function(response) {
        var resp = JSON.parse(response),
            p_parent = document.getElementsByClassName('p_parent'),
            p = p_parent.length;

            while(p--) p_parent[p].remove();

        if(resp.length > 0) {
            downlines.style.display = 'initial'
            downlines.children[0].children[0].innerHTML = e.innerHTML;

            for(var i = 0; i < resp.length; i++) {
                var p_parent = document.createElement('div'),
                    p1 = document.createElement('p'),
                    p2 = document.createElement('p'),
                    p3 = document.createElement('p'),
                    p4 = document.createElement('p');

                p_parent.classList.add('p_parent');
                p1.innerHTML = resp[i].id;
                p2.innerHTML = resp[i].account_code;
                p3.innerHTML = resp[i].created_by;
                p4.innerHTML = resp[i].updated_at;
                p_parent.appendChild(p1);
                p_parent.appendChild(p2);
                p_parent.appendChild(p3);
                p_parent.appendChild(p4);
                downlines_holder.appendChild(p_parent);
            }
        } else {
            downlines.style.display = 'none'
        }
    }, function(error) {
        console.log(error);
    })
}

我正在寻找同样的问题并且找不到。

任何帮助将不胜感激。感谢!!!

1 个答案:

答案 0 :(得分:0)

请在返回下线之前执行此操作

public function getDownlines($id)
{
  $upline = Upline::find($id);

  return $upline->downlines->map(function($downline) {
    return [
      'id' => $downline->id,
      'account_code' => $downline->account_code,
      'created_by' => $downline->created_by,
      'created_at' => $downline->created_at->diffForHumans(),
      'updated_at' => $downline->updated_at->diffForHumans(),
    ];
  });
}

我不确定您是否要使用created_atupdated_at,因为在html中您已编写<p>Created At</p>但在AJAX请求中,您已编写p4.innerHTML = resp[i].updated_at;。所以我在返回数组中添加了两个:)