无法获取 user_id 值 Laravel 查询

Can't get user_id value Laravel Query

当我将 select('name') 更改为 select('user_id' 时,我无法使用数据表在我的视图页面中调用 user_id ) 或 select('*') 它也显示错误 DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7 我的代码有什么问题?提前致谢

我有一个包含以下代码的控制器

public function TeamTask(Request $request)
    {
      
        if ($request->ajax()) {
            $data = DB::table('posts')->select('name')->selectraw('count(user_id) as total')->selectRaw('SUM(status = "Done") as done')->where('div',  Auth::user()->div)->groupBy('name')->get();
            return Datatables::of($data)
                ->addColumn('action', function ($row) {

                    $btn = ' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="' . $row->name . '" data-original-title="Detail" class="btn btn-success mr-1 btn-sm detailProduct"><span class="fas fa-info"></span></a>';

                    return $btn;
                })
                ->rawColumns(['action'])
                ->addIndexColumn()
                ->make(true);
        }

        return view('task.teamTask',);
    }

我的观点

<script type="text/javascript">
    $(function() {

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        var table = $('.data-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: "{{ route('team.task') }}",

            columns: [{
                    data: 'DT_RowIndex',
                    name: 'DT_RowIndex',
                    orderable: false,
                    searchable: false,
                },
                {
                    data: 'name',
                    name: 'name',
                    orderable: false,
                },
                {
                    data: 'user_id',
                    name: 'user_id',
                    orderable: false,
                },
                {
                    data: 'total',
                    name: 'total',
                    orderable: false,
                },
                {
                    data: 'done',
                    name: 'done'
                },
                {
                    data: 'action',
                    name: 'action'
                },

            ]
        });

    });
</script>

当我将 select('name') 更改为 select('user_id' 时,我无法使用数据表在我的视图页面中调用 user_id ) 或 select('*') 它也显示错误 DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7 我的代码有什么问题?提前致谢

试试这个。

public function TeamTask(Request $request)
    {
      
        if ($request->ajax()) {
            $data = DB::table('posts')->select('name','user_id')->selectraw('count(user_id) as total')->selectRaw('SUM(status = "Done") as done')->where('div',  Auth::user()->div)->groupBy('name')->get();
            return Datatables::of($data)
                ->addColumn('action', function ($row) {

                    $btn = ' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="' . $row->name . '" data-original-title="Detail" class="btn btn-success mr-1 btn-sm detailProduct"><span class="fas fa-info"></span></a>';

                    return $btn;
                })
                ->rawColumns(['action'])
                ->addIndexColumn()
                ->make(true);
        }

        return view('task.teamTask',);
    }

在您的查询中添加 ->groupBy('user_id'),这应该可以解决您的问题