问题
So in this app Drawing
belongsTo
Customer
. I have datatable
<table id='drawing-table' class="table table-bordered table-hover">
<thead>
<tr>
<th>Drawing number</th>
<th>Customer</th>
</tr>
</thead>
</table>
which indicates $darwing->number
and $customer->title
. To load info I use yajra\Datatables\Datatables;
.
Data is loaded with this JS method:
$(function () {
$('#drawing-table').DataTable({
processing: true,
serverSide: true,
ajax: '{{route('drawings.datatable')}}',
columns: [
{ data: 'number', name: 'number' },
{ data: 'customer.title', name: 'customer' },
]
});
});
And this Laravel method:
public function datatable()
{
$drawings = Drawing::select(array('drawings.id','drawings.number'));
return Datatables::of(Drawing::with('customer')->select('*'))->make(true);
}
QUESTIONS
- How do I make datatable search window to work with
$customer->title
? - How do I display drawing number and customer title as link?
回答1:
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);
}
Spent many hours trying to figure it out, hope it saves someone time. http://datatables.yajrabox.com/fluent/joins
回答2:
I'm not really sure about your first question. Datatables search window will search all the content. Do you want to make it specific to just 1 column?
To answer your second question, you can edit the column output. Try this
$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);
Edit
To achieve the search that you want, You'll want to do something like this:
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);
}
Then, in your 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' },
]
});
});
This is untested, but should achieve what you want.
回答3:
you may also use elequent relationships with yajra here is the example code.
$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'];
});
in this example you can get sector against sub sector with each method you can get sector name and now you can display sector in your yajra table
来源:https://stackoverflow.com/questions/33104378/laravel-datatable-relationships