2017-10-27 4 views
0

이 작업을 다시 시도했지만 제대로 작동했지만 이제는 "hasMany"관계를 호출하여 테이블에 표시해야합니다. 이것은 내 컨트롤러입니다 :Datatables (yajrabox) 중첩 된 객체 표시 (laravel hasMany relationship)

public function anyData() 
{ 

    $posts = Test::with('cola')->select('tests.*'); 

    return \DataTables::eloquent($posts) 

    ->make(true);} 

는 나에게이 (testanyData 경로)와 같은 배열을 제공 :

$(document).ready(function(){ 
       listar(); 
     }); 
     var listar = function(){ 
       var table = $('#productos').DataTable({ 
         "processing": true, 
         "serverSide": true, 
         "ajax": "testanyData", 
         "columns":[ 
           {data:'id'}, 
           {data:'nombre'}, 
           {data:'stock'}, 
           {data: 'cola.precio'}, 
           {defaultContent: 
             "some buttons" 
           } 
         ], 
         "language": idioma_esp 

       }); 


} 
:

{ 
"draw": 0, 
"recordsTotal": 1, 
"recordsFiltered": 1, 
"data": [ 
{ 
"id": "1", 
"nombre": "Sigrid Mann", 
"descripcion": "lPcA0", 
"stock": "49", 
"imagen": "s14gN", 
"created_at": "2017-10-27 18:35:54", 
"updated_at": "2017-10-27 18:35:54", 
"cola": [ 
{ 
"test_id": "1", 
"tipo": "1", 
"precio": "1", 
"created_at": null, 
"updated_at": null 
} 
    ] 
} 
], 
"queries": [ 
{ 
"query": "select count(*) as aggregate from (select '1' as `row_count` from `tests`) count_row_table", 
"bindings": [], 
"time": 0.6 
}, 
{ 
"query": "select `tests`.* from `tests`", 
"bindings": [], 
"time": 0.46 
}, 
{ 
"query": "select * from `precios` where `precios`.`test_id` in (?)", 
"bindings": [ 
1 
], 
"time": 0.52 
} 
], 
"input": [] 
} 

이 내 JS입니다 내가보기를로드 할 때 3,691,363,210

는하지만 난이와 경고를 얻을 :

DataTables 경고 : 테이블 ID = PRODUCTOS - 알 수없는 매개 변수 행 0, 열 (3)에 대한 'cola.precio을'요청에 대한 자세한 내용은

<div class="row-fluid margin-body"> 
       <table id="productos" class="table table-hover table-condensed " > 
         <thead> 
           <tr> 
             <th >Id</th> 
             <th>Producto</th> 
             <th>Stock </th> 
             <th>Precio</th> 
             <th></th> 
           </tr> 
         </thead> 
       </table> 
     </div> 

내가 t에서 "precio"표시 할이 오류,이보기에 내 테이블입니다 http://datatables.net/tn/4

를 참조하십시오 그는 태그 "Precio".

답변

0

응답에 표시된대로 배열을 반환하기 때문에 콜라 관계가 hasMany 인 것처럼 보입니다. 이와 관련

"cola": [ { "test_id": "1", "tipo": "1", "precio": "1", "created_at": null, "updated_at": null } ]

은 또한 배열로 데이터를 액세스한다.

{data: 'cola.0.precio', name: 'cola.precio'},

또는 당신은 콜라의 모든 가능한 값에 dataTables.js 라이브러리와 루프의 render API를 사용하여 적절하게 렌더링 할 수 있습니다.

+0

정말 고마워요. 제게 미치겠습니다. –