v2.5.2
Giriş yap

belongTo ajax fetchdatada nasıl çalışır?

goxaria
271 defa görüntülendi

Sitemde datanın canlı güncelleneceği bir bölge yapıyorum ve birbirleriyle ilişkili tablolarım var. Bunları belongsTo ile bağlayıp zincir method şeklinde çağırabiliyordum.

Örneğin

    {{$makale->posts->title}}

Ancak bunu ajax ile fetch ederken yapamıyorum. Sadece json değeri olarak belongsto ignorelanmış hali dönüyor verimin ki bu çok normal kodumu nasıl düzeltirim acaba;

public function fetchdata(){
    $comments = Comment::groupBy('comments.post_id')
        ->orderBy(\DB::raw('count(comments.post_id)'), 'DESC')
        ->where('created_at', '>=', Carbon::now()->subDay())
        ->take(5)
        ->get();

    return response()->json([
        'comments' => $comments,
    ]);
}
$(document).ready(function () {
   fetchdata();

   function fetchdata(){
       $.ajax({
           type: "GET",
           url: "/fetchdata",
           dataType: "json",
           success: function (response){
               $.each(response.comments, function (key, item){
                    $('.topics').append(
                        '<div class="topic">\
                                <h3>'+item.post.title+'</h3>\
                            </div>'
                    );
               });
           }
       });
   }

});
Cevap yaz
Cevaplar (0)
Henüz kimse cevap yazmadı. İlk cevap yazan sen ol!