2014-12-01 2 views
0

제품 테이블에 설정된 카테고리 테이블의 이름을 외래 키 categories_id로 표시하려고합니다. 것은 내가 Laravel 4 (https://github.com/bllim/laravel4-datatables-package)에 대한 billm 렌더링 패키지와 함께 datatables jquery plugin (http://www.datatables.net/)을 사용하고 있습니다.데이터 테이블을 통해 숫자 값 대신 FK 이름을 표시합니다.

public function getInventory() 
{ 
    $inventory = Inventory::select(array('inventory.id', 'inventory.code', 'inventory.name', 'inventory.price', 'inventory.cost', 'inventory.categories_id')); 

    return Datatables::of($inventory) 
     ->add_column('operations', 
      '<a class="btn btn-xs dark btn-trash tooltips" data-toggle="modal" data-target=".edit-inventory-modal{{ $id }}"><i class="fa fa-pencil"></i></a> 
      <a class="btn btn-xs dark btn-trash tooltips" data-toggle="modal" data-target=".delete-inventory-modal{{ $id }}"><i class="fa fa-trash-o"></i></a>') 
     ->make(); 
} 

그리고 나는 나의보기에 표시 :

현재 내 컨트롤러 InventoryController에 인쇄있어 JQuery와 테이블을로드 할 수있는 적절한 스크립트를

<div class="row"> 
    <div class="col-lg-12"> 
     <a class="btn btn-small btn-primary col-md-offset-10" href="#" data-toggle="modal" data-target=".create-inventory-modal">Crear producto</a><br /><br /> 
     <div class="table-responsive"> 
      <table class="table table-striped table-bordered table-hover" id="dataTable-inventory"> 
       <thead> 
        <tr> 
         <th>ID</th> 
         <th>Código</th> 
         <th>Nombre</th> 
         <th>Precio</th> 
         <th>Costo</th> 
         <th>Categoría</th> 
         <th>Operaciones</th> 
        </tr> 
       </thead> 
      </table> 
     </div> 
     <!-- /.table-responsive --> 
    </div> 
    <!-- /.col-lg-12 --> 
</div> 
<!-- /.row --> 

:

@section('script') 
<script src="js/plugins/dataTables/jquery.dataTables.js"></script> 
<script src="js/plugins/dataTables/dataTables.bootstrap.js"></script> 
<script> 

    $(document).ready(function() { 

     var oTable = $('#dataTable-inventory').dataTable({ 
      "bProcessing": true, 
      "bServerSide": true, 
      "sAjaxSource": "{{ URL::to('inventario.tabla') }}" 
     }) 

경로가 getInventory()로 이동하는 곳

Route::get('inventario.tabla', array('uses' => '[email protected]', 'as' => 'inventario.tabla')); 

그리고 모든 것은 정상적으로 작동하지만 데이터 테이블에서는 숫자가 아니라 이름 만 표시됩니다. 내가 여기에 합류하는 걸보고 있니?

답변

0

내가해야하는 일은 설명서에 표시된대로 데이터 테이블 플러그인의 구조로 조인 문을 사용하기 만하면됩니다.

public function getInventory() 
{ 
    $inventory = Inventory::select(array('inventory.id', 
             'inventory.code', 
             'inventory.name', 
             'inventory.price', 
             'inventory.cost', 
             'categories.name as category_name')) 
     ->leftJoin('categories','inventory.categories_id','=','categories.id'); 

    return Datatables::of($inventory) 
     ->add_column('operations', 
      '<a class="btn btn-xs dark btn-trash tooltips" data-toggle="modal" data-target=".edit-inventory-modal{{ $id }}"><i class="fa fa-pencil"></i></a> 
      <a class="btn btn-xs dark btn-trash tooltips" data-toggle="modal" data-target=".delete-inventory-modal{{ $id }}"><i class="fa fa-trash-o"></i></a>') 
     ->make(); 
} 
관련 문제