好的,我的用户具有不同的角色,我想知道如何在数据表中显示而不是角色的ID,我想查看角色的名称..
public function index(Request $request)
{
// $users = User::orderBy('id', 'ASC')->paginate(5);
//return view('admin.users.index', ['users' => $users]);
if($request->ajax()){
$data = User::latest()->get();
return DataTables::of($data)
->addColumn('accion',function($data){
$button = '<button type="button" name="edit" id="'.$data->id.'" class="edit btn btn-primary btn-sm">Edit</button>';
$button .= '<button type="button" name="delete" id="'.$data->id.'" class="delete btn btn-danger btn-sm">Delete</button>';
return $button;
})
->rawColumns(['accion'])
->make(true);
}
return view('admin.users.index');
}
datatable.js
$(document).ready( function () {
$('#table').DataTable({
processing: true,
serverSide: true,
ajax: "/admin/users/",
columns:
[
{
data: 'id',
name: 'id',
},
{
data:'name',
name: 'name',
},
{
data:'email',
name: 'email',
},
{
data:'role_id',
name: 'role_id',
},
{
data:'accion',
name: 'accion',
orderable:false
},
]
});
} );
它显示了角色的ID,但我想访问角色的name属性
答案 0 :(得分:0)
首先,您要确保在查询用户数据时正在加载角色信息,以防止重复查询
$data = User::with('role')->latest()->get();
然后将role.name添加到数据表中:
{
data: 'role.name',
name: 'role'
}
这些示例假定您已经在用户模型上建立了关系,并将其命名为“角色”。
答案 1 :(得分:0)
从控制器或路由开始,仅是一个例子
Route::get('/table_data', function (Request $request) {
$columns = array(
0 => 'id',
1 => 'key',
2 => 'doc_name',
3 => 'name',
4 => 'email',
5 => 'phone',
6 => 'city',
7 => 'options'
);
//Below are some code for pagination of datatable and counts etc...
$totalData = Patient::count();
$totalFiltered = $totalData;
$limit = $request->input('length');
$start = $request->input('start');
$order = $columns[$request->input('order.0.column')];
$dir = $request->input('order.0.dir');
if(empty($request->input('search.value'))){
$patients = Patient::offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
}
else
{
//Below code is for search facility real time searching in datatable
$search = $request->input('search.value');
$patients = Patient::with('doctor')
->where('name', 'LIKE', "%{$search}%")
->orWhere([
['email', 'LIKE', "%{$search}%"],
['phone', 'LIKE', "%{$search}%"],
['city', 'LIKE', "%{$search}%"]
])
->offset($start)
->limit($limit)
->orderBy($order,$dir)->get();
$totalFiltered = Patient::with('doctor')->where('name','LIKE',"%{$search}%")->orWhere([
['email', 'LIKE', "%{$search}%"],
['phone', 'LIKE', "%{$search}%"],
['city', 'LIKE', "%{$search}%"]
])->count();
}
$data = array();
if(!empty($patients))
{
foreach ($patients as $key=>$patient)
{
$nestedData['id'] = $patient->id;
$nestedData['key'] = $key;
//Here this is what I wanted to point out for you, it's a relationship, like in your case it will be something like $user->role->name
$nestedData['doc_name'] = $patient->doctor->name;
$nestedData['name'] = $patient->name;
$nestedData['email'] = $patient->email;
$nestedData['phone'] = $patient->phone;
$nestedData['city'] = $patient->city;
$nestedData['options'] = '<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu edit-options dropdown-menu-right dropdown-default" user="menu">
<li>
<button="type" class="btn btn-link view"><i class="fa fa-eye"></i> View</button>
</li><li>
<a href="#" class="btn btn-link"><i class="fa fa-edit"></i> Edit</a>
</li><li>
<button type="submit" class="btn btn-link" onclick="return confirmDelete()"><i class="fa fa-trash"></i> Delete</button>
</li>
</ul>
</div>';
$nestedData['patient'] = array( '[ "'.$nestedData['id'].'"', ' "'.$nestedData['key'].'"', ' "'.$nestedData['name'].'"', ' "'.$nestedData['email'].'"', ' "'.$nestedData['phone'].'"', ' "'.$nestedData['city'].'"]'
);
$data[] = $nestedData;
}
}
$json_data = array(
"draw" => intval($request->input('draw')),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFiltered),
"data" => $data
);
echo json_encode($json_data);
});
在数据表中,您将执行以下操作
$('#example').DataTable( {
"processing": true,
"serverSide": true,
// "scrollY": 200,
// "deferRender": true,
// "scroller": true,
"ajax":{
url:"{{ url('table_data') }}",
dataType: "json",
type:"get"
},
"createdRow": function( row, data, dataIndex ) {
$(row).addClass('patient-link');
$(row).attr('data-patient', data['patient']);
},
"columns": [
{"data": "key"},
{"data": "doc_name"},
{"data": "name"},
{"data": "email"},
{"data": "phone"},
{"data": "city"},
{"data": "options"}
],
'language': {
'searchPlaceholder': "Type patient name",
'lengthMenu': '_MENU_ records per page',
"info": 'Showing _START_ - _END_ (_TOTAL_)',
"search": ' Search',
'paginate': {
'previous': 'Previous',
'next': 'Next'
}
},
'columnDefs': [
{
orderable: false,
targets: [0, 6],
},
{
'targets': 0
}
],
order:[[1, 'asc']],
'lengthMenu': [[10, 25, 50, -1], [10, 25, 50, "All"]],
dom: '<"row"lfB>rtip'
} );
注意:我粘贴了这段完整的代码,因为数据表在laravel中花费了很多时间,您也可以添加复选框和按钮。而且我认为这段代码将帮助很多真正使用Laravel的人使用数据表,最重要的是,只需启用它的库就可以包括数据表的全部功能,一切都会正常工作