2016-11-26 27 views
0

私はlaravelとネイティブのajaxで作業しています。私はどこにajaxを使用するときdiffForhHumans()を置くのだろうかと思います。私のコントローラーで。私はオブジェクトのフェッチを返します。aJaxを使用してlaravelでdiffForHumans()を適用するにはどうすればよいですか?

ここに私のコントローラ

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> 

アヤックス

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); 
    }) 
} 
中のスクリプトがあります

私は同じ問題を探していて、見つけられません。

ご協力いただければ幸いです。ありがとう!!!

答えて

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

あなたがHTMLにあなたが<p>Created At</p>を書かれているが、AJAXリクエストで、あなたはp4.innerHTML = resp[i].updated_at;を書かれているので、created_atまたはupdated_atを、使用したい場合、私はわからないよを返す前にこれを行ってください。だから私はリターン配列で両方を追加しました:

+0

ありがとう。それは働いたが、私は訂正をしている。これは多くの助けになるので。 'diffInHumans()'を 'diffForHumans()'に変更してください –

+0

@HermanLunaはい、私はそれを完全に忘れてしまいました。今私の答えを編集:)ありがとう:) – prateekkathal

関連する問題