laravel 数据表关系

laravel datatable relationships

所以在这个应用中 Drawing belongsTo Customer。我有数据表

            <table id='drawing-table' class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>Drawing number</th>
                        <th>Customer</th>
                    </tr>
                </thead>
            </table>

表示$darwing->number$customer->title。加载信息我 use yajra\Datatables\Datatables;.

数据是用这个JS方法加载的:

$(function () {
    $('#drawing-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{{route('drawings.datatable')}}',
        columns: [
            { data: 'number', name: 'number' },
            { data: 'customer.title', name: 'customer' },
        ]
    });
});

而这个Laravel方法:

public function datatable()
{
    $drawings = Drawing::select(array('drawings.id','drawings.number'));

    return Datatables::of(Drawing::with('customer')->select('*'))->make(true);
}

问题

  1. 如何使数据表搜索 window 与 $customer->title 一起使用?
  2. 如何将图号和客户标题显示为 link?

我不太确定你的第一个问题。 Datatables search window 将搜索所有内容。是否仅针对 1 列?

要回答您的第二个问题,您可以编辑列输出。试试这个

$drawings = Drawing::select(array('drawings.id','drawings.number'));

return Datatables::of(Drawing::with('customer')->select('*'))
        ->editColumn('customer', function($drawings) {
            return '<a href="#">' . $drawings->customer . '</a>';
        })              
        ->make(true);

编辑

要实现您想要的搜索,您需要执行以下操作:

public function datatable(Request $request)
{
    $drawings = Drawing::select(array('drawings.id','drawings.number'));

    return Datatables::of(Drawing::with('customer')->select('*'))
            ->filter(function ($query) use ($request) {
                if ($request->has('name')) {
                    $query->where('customer.customer_name', 'like', "%{$request->get('name')}%");
                }
            })
            ->editColumn('customer', function($drawings) {
                return '<a href="#">' . $drawings->customer->customer_name . '</a>';
            })              
            ->make(true);
}

然后,在你的 JS 中

$(function () {
    $('#drawing-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '{{route('drawings.datatable')}}',
            data: function (d) {
                d.name = $('input[name=name]').val();
            }
        },
        columns: [
            { data: 'number', name: 'number' },
            { data: 'customer.title', name: 'customer' },
        ]
    });
});

这是未经测试的,但应该能达到你想要的效果。

    public function datatable()
    {

        //reference customer table
        $drawings = DB::table('customers')
            // join it with drawing table
            ->join('drawings', 'drawings.customer_id', '=', 'customers.id')
            //select columns for new virtual table. ID columns must be renamed, because they have the same title
            ->select(['drawings.id AS drawing_id', 'drawings.number', 'customers.title', 'customers.id AS customer_id']);

        // feed new virtual table to datatables and let it preform rest of the query (like, limit, skip, order etc.)
        return Datatables::of($drawings)
            ->editColumn('title', function($drawings) {
                return '<a href="'.route('customers.show', $drawings->customer_id).'">' . $drawings->title . '</a>';
            })  
            ->editColumn('number', function($drawings) {
                return '<a href="'.route('drawings.show', $drawings->drawing_id).'">' . $drawings->number . '</a>';
            })  
            ->make(true);
    }

花了很多时间试图弄明白,希望它能节省一些人的时间。 http://datatables.yajrabox.com/fluent/joins

您也可以使用与 yajra 的 Elequent 关系,这里是示例代码。

$sub_sectors = Sector::where('parent_id', '>', 0)->with('parent')->latest()->get();
$sub_sectors->each(function($sub_sectors){
    $sub_sectors->sector = $sub_sectors->parent->title['en'];
});

在此示例中,您可以使用每种方法获取针对子扇区的扇区您可以获得扇区名称,现在您可以在您的 yajra 中显示扇区 table