【问题标题】:How to apply diffForHumans() in laravel using ajax?如何使用 ajax 在 laravel 中应用 diffForHumans()?
【发布时间】:2016-11-26 06:51:22
【问题描述】:

我正在使用 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);
    })
}

我正在寻找同样的问题,但没有找到。

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

【问题讨论】:

    标签: ajax laravel


    【解决方案1】:

    请在返回下线之前完成此操作

    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_at还是updated_at,因为在html中你写了&lt;p&gt;Created At&lt;/p&gt;但在AJAX请求中你写了p4.innerHTML = resp[i].updated_at;。所以我在返回数组中都添加了:)

    【讨论】:

    • 谢谢。它有效,但我有一个更正。因为这可以帮助很多。将diffInHumans() 更改为diffForHumans()
    • @HermanLuna 是的,我完全忘记了。现在编辑我的答案:)谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2019-06-13
    • 2017-08-27
    • 1970-01-01
    • 2017-10-23
    • 2019-06-20
    • 2015-09-21
    • 2013-07-05
    • 2017-06-28
    相关资源
    最近更新 更多