这是我的控制器中的ajax请求:
public function dtajax()
{
$users = User::all();
$users = DB::table('tc_users')
->join('users_tc_users','users_tc_users.tc_users_id', '=','tc_users.id')
->join('users','users.id', '=','users_tc_users.users_id')
->leftJoin('cars','cars.users_id', '=','users.id')
->groupBy('users.id')
->select(DB::raw("users.id as id , users.name as name , users.email as email , users.Expiration_Date as Expiration_Date , users.phone_number as phone_number ,users.allowedcar as allowedcar ,COUNT(cars.id) as cars_count "))
->get(array("users.id as id , users.name as name , users.email as email , users.Expiration_Date as Expiration_Date , users.phone_number as phone_number ,users.allowedcar as allowedcar,COUNT(cars.id) as cars_count "));
foreach ($users as $user){
echo $user->cars_count;
}
$values = DB::table('users')->select($this->listing_cols)->whereNull('deleted_at');
$out = Datatables::of($values)->make();
$data = $out->getData();
$fields_popup = ModuleFields::getModuleFields('Users');
for($i=0; $i < count($data->data); $i++) {
for ($j=0; $j < count($this->listing_cols); $j++) {
$col = $this->listing_cols[$j];
// $this->listing_cols->kk;
// echo $col['no_of_cars'];
if($fields_popup[$col] != null && starts_with($fields_popup[$col]->popup_vals, "@")) {
$data->data[$i][$j] = ModuleFields::getFieldValue($fields_popup[$col], $users, $data->data[$i][$j]);
// $data->data[$i][$j] .= 'yssss';
}
if($col == $this->view_col) {
$data->data[$i][$j] = '<a href="'.url(config('laraadmin.adminRoute') . '/users/'.$data->data[$i][0]).'">'.$data->data[$i][$j].'</a>';
}
// else if($col == "author") {
// $data->data[$i][$j];
// }
}
if($this->show_action) {
$output = '';
if(Module::hasAccess("Users", "edit")) {
$output .= '<a href="'.url(config('laraadmin.adminRoute') . '/users/'.$data->data[$i][0].'/edit').'" class="btn btn-warning btn-xs" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-edit"></i></a>';
}
if(Module::hasAccess("Users", "delete")) {
$output .= Form::open(['route' => [config('laraadmin.adminRoute') . '.users.destroy', $data->data[$i][0]], 'method' => 'delete', 'style'=>'display:inline']);
$output .= ' <button class="btn btn-danger btn-xs" type="submit"><i class="fa fa-times"></i></button>';
$output .= Form::close();
}
$data->data[$i][] = (string)$output;
}
}
$out->setData($data);
return $out;
}
这就是前面的ajax函数:
$(function () {
$("#example1").DataTable({
processing: true,
serverSide: true,
ajax: "{{ url(config('laraadmin.adminRoute') . '/user_dt_ajax') }}",
language: {
lengthMenu: "_MENU_",
search: "_INPUT_",
searchPlaceholder: "Search"
},
@if($show_actions)
columnDefs: [ { orderable: false, targets: [-1] }],
@endif
});
$("#user-add-form").validate({
});
});
</script>
我想要的只是添加一个名为cars_count
的新列,并从上面的选择查询中插入其值,我可以通过以下方式将其回显
foreach ($users as $user){
echo $user->cars_count;
}
我无法从视图中填充表格,因为它是根据基本数据从ajax请求中填充的