通过数据表显示FK的名称而不是数值

时间:2014-12-01 20:58:53

标签: twitter-bootstrap laravel-4 jquery-datatables

我试图将产品表中设置的类别表的名称显示为外键categories_id。问题是我也在使用数据表jquery插件(http://www.datatables.net/)和Laravel 4(https://github.com/bllim/laravel4-datatables-package)的billm渲染包。

目前我在我的控制器InventoryController上打印这个:

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

我将其显示在我的视图中:

<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 -->

使用适当的脚本加载Jquery表:

@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' => 'InventoryController@getInventory', 'as' => 'inventario.tabla'));

所有这一切都运行正常,但在数据表中它只显示数字,而不是名称。我在看这里加入吗?

1 个答案:

答案 0 :(得分:0)

我所要做的只是使用一个带有datatable插件结构的join语句,如其手册所示。

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