具有Laravel 7的数据词,来自show方法的URL ID不起作用

发布于 2025-01-23 14:09:48 字数 4490 浏览 0 评论 0原文

我得到了一个学校项目,其中我们使用了Laraval 7。在此项目中,我使用的是数据表,并尝试从目前登录的顾问中根据timesheet_id的特定时间表显示工作日。现在,当我将此ID作为整数硬化时,它按预期工作,但是当我检索URL中给出的ID并将其放入变量时,它不接受它,并且根本没有记录。

这起作用但进行了硬编码,当我在显示方法中具有整数值的变量并使用它时,它也有效:

public function show($id)
    {
        if (request()->ajax()) {
            $data = (Workday::where('timesheet_id', 1)->where('user_id', Auth::id())->with('timesheet.consultantOpportunity.consultant', 'dayType', 'timesheet.consultantOpportunity.opportunity.customer'));
            return datatables()->of($data)
                ->addColumn('timesheet', function ($data) {
                    return $data->timesheet->id;
                })
                ...
                ->addColumn('updated_at', function ($data) {
                    $updated_at = $data->updated_at ? (new DateTime($data->updated_at))->format('d-m-Y') : null;
                    return $updated_at;
                })
                ->rawColumns([ 'timesheet', 'client', 'opportunity', 'contact_person', 'day_type', 'date_sort', 'date', 'start', 'end', 'duration', 'pause', 'total', 'comment', 'actionButtons', 'updated_at'])
                ->addIndexColumn()
                ->make(true);
        }
        return view('consultants.timesheets.workdays.index');
    }

[它导致了这一点:] [1]

,当我使用从url中给出的整数时, 't根本没有告诉我任何记录: [这是返回$ id给我的:] [2]

public function show($id)
    {
        if (request()->ajax()) {
            $data = (Workday::where('timesheet_id', $id)->where('user_id', Auth::id())->with('timesheet.consultantOpportunity.consultant', 'dayType', 'timesheet.consultantOpportunity.opportunity.customer'));
            return datatables()->of($data)
                ->addColumn('timesheet', function ($data) {
                    return $data->timesheet->id;
                })
                ...
                ->addColumn('updated_at', function ($data) {
                    $updated_at = $data->updated_at ? (new DateTime($data->updated_at))->format('d-m-Y') : null;
                    return $updated_at;
                })
                ->rawColumns([ 'timesheet', 'client', 'opportunity', 'contact_person', 'day_type', 'date_sort', 'date', 'start', 'end', 'duration', 'pause', 'total', 'comment', 'actionButtons', 'updated_at'])
                ->addIndexColumn()
                ->make(true);
        }
        return view('consultants.timesheets.workdays.index');
    }

[导致这样的:] [3]

我的数据词就是这样:

<script>
        $(document).ready( function () {
            $('#myTable').DataTable({
                processing: true,
                serverSide: false,
                ajax: "{{ route( 'workdays.show', 'id') }}",
                order: ([2, 'des']),
                columns: [
                    { data: 'timesheet', name: 'timesheet', visible: true },
                    { data: 'id', name: 'id', visible: false },
                    { data: 'day', name: 'day', orderable: false, visible: true },
                    { data: 'date_sort', name: 'date_sort', visible: false,},
                    { data: 'date', name: 'date', orderData: [ 2 ] },
                    { data: 'day_type', name: 'day_type' },
                    { data: 'client', name: 'client' , visible: true},
                    { data: 'opportunity', name: 'opportunity' , visible: true},
                    { data: 'start', name: 'start' },
                    { data: 'end', name: 'end' },
                    { data: 'duration', name: 'duration' },
                    { data: 'pause', name: 'pause' },
                    { data: 'comment', name: 'comment', orderable: false},
                    { data: 'total', name: 'total'},
                    // { data: 'confirm', name: 'confirm'},
                    { data: 'actionButtons', name: 'actionButtons'},
                    { data: 'updated_at', name: 'updated_at'},
                ],
            });
        });
</script>

我的路由只是一条资源路由。 我希望有人可以通过简单的工作解决方案来帮助我。提前致谢 :) [1]: https://i.sstatic.net/266rj.jpg [2]: https://i.sstatic.net/uokwg.jpg [3]: https://i.sstatic.net/kckbo.jpg

I got a school project where we use Laraval 7. In this project I'm using DataTables and try to show the worked days from a specific timesheet based on a timesheet_id, from the currently logged-in consultant. Now when I hardcore this id as integer it works as intended but when I retrieve the id given in the URL and drop it in as variable it does not accept it and shows me no records at all.

This works but is hardcoded, it also works when I make a variable with an integer value in the show method itself and use that:

public function show($id)
    {
        if (request()->ajax()) {
            $data = (Workday::where('timesheet_id', 1)->where('user_id', Auth::id())->with('timesheet.consultantOpportunity.consultant', 'dayType', 'timesheet.consultantOpportunity.opportunity.customer'));
            return datatables()->of($data)
                ->addColumn('timesheet', function ($data) {
                    return $data->timesheet->id;
                })
                ...
                ->addColumn('updated_at', function ($data) {
                    $updated_at = $data->updated_at ? (new DateTime($data->updated_at))->format('d-m-Y') : null;
                    return $updated_at;
                })
                ->rawColumns([ 'timesheet', 'client', 'opportunity', 'contact_person', 'day_type', 'date_sort', 'date', 'start', 'end', 'duration', 'pause', 'total', 'comment', 'actionButtons', 'updated_at'])
                ->addIndexColumn()
                ->make(true);
        }
        return view('consultants.timesheets.workdays.index');
    }

[It results in this:][1]

And here when I use the integer given from the URL it doesn't show me no records at all:
[This is what return $id gives me:][2]

public function show($id)
    {
        if (request()->ajax()) {
            $data = (Workday::where('timesheet_id', $id)->where('user_id', Auth::id())->with('timesheet.consultantOpportunity.consultant', 'dayType', 'timesheet.consultantOpportunity.opportunity.customer'));
            return datatables()->of($data)
                ->addColumn('timesheet', function ($data) {
                    return $data->timesheet->id;
                })
                ...
                ->addColumn('updated_at', function ($data) {
                    $updated_at = $data->updated_at ? (new DateTime($data->updated_at))->format('d-m-Y') : null;
                    return $updated_at;
                })
                ->rawColumns([ 'timesheet', 'client', 'opportunity', 'contact_person', 'day_type', 'date_sort', 'date', 'start', 'end', 'duration', 'pause', 'total', 'comment', 'actionButtons', 'updated_at'])
                ->addIndexColumn()
                ->make(true);
        }
        return view('consultants.timesheets.workdays.index');
    }

[Resulting in this:][3]

My DataTable is just like this:

<script>
        $(document).ready( function () {
            $('#myTable').DataTable({
                processing: true,
                serverSide: false,
                ajax: "{{ route( 'workdays.show', 'id') }}",
                order: ([2, 'des']),
                columns: [
                    { data: 'timesheet', name: 'timesheet', visible: true },
                    { data: 'id', name: 'id', visible: false },
                    { data: 'day', name: 'day', orderable: false, visible: true },
                    { data: 'date_sort', name: 'date_sort', visible: false,},
                    { data: 'date', name: 'date', orderData: [ 2 ] },
                    { data: 'day_type', name: 'day_type' },
                    { data: 'client', name: 'client' , visible: true},
                    { data: 'opportunity', name: 'opportunity' , visible: true},
                    { data: 'start', name: 'start' },
                    { data: 'end', name: 'end' },
                    { data: 'duration', name: 'duration' },
                    { data: 'pause', name: 'pause' },
                    { data: 'comment', name: 'comment', orderable: false},
                    { data: 'total', name: 'total'},
                    // { data: 'confirm', name: 'confirm'},
                    { data: 'actionButtons', name: 'actionButtons'},
                    { data: 'updated_at', name: 'updated_at'},
                ],
            });
        });
</script>

My route is just a resource route.
I hope someone can help me out with a simple working solution. Thanks in advance :)
[1]: https://i.sstatic.net/266Rj.jpg
[2]: https://i.sstatic.net/UOKWG.jpg
[3]: https://i.sstatic.net/KCkbo.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清旖 2025-01-30 14:09:48

通过这样做找到解决方案:

$(document).ready( function () {
        $('#myTable').DataTable({
                processing: true,
                serverSide: false,
                ajax: '/consultant/timesheets/workdays/'+parseInt(window.location.href.split('/')[6].replace('#!', '')),
                order: ([3, 'des']),
                columns: [
                    { data: 'timesheet_id', name: 'timesheet_id', visible: false},
                    { data: 'id', name: 'id', visible: false },
                    { data: 'day', name: 'day', orderable: false, visible: true},
                    { data: 'date_sort', name: 'date_sort', visible: false},
                    { data: 'date', name: 'date', orderData: [ 3 ] },
                    { data: 'day_type', name: 'day_type' },
                    { data: 'client', name: 'client' , visible: true},
                    { data: 'opportunity', name: 'opportunity' , visible: true},
                    { data: 'start', name: 'start' },
                    { data: 'end', name: 'end' },
                    { data: 'duration', name: 'duration', visible: false },
                    { data: 'pause', name: 'pause' },
                    //{ data: 'comment', name: 'comment', orderable: false},
                    { data: 'total', name: 'total'},
                    // { data: 'confirm', name: 'confirm'},
                    { data: 'actionButtons', name: 'actionButtons'},
                    //{ data: 'updated_at', name: 'updated_at'},
                ],
            });
        });

它抓住URL并从中获取ID将其传递到Ajax路线。然后,我可以将ID传递给控制器​​并在查询中使用。

Found a solution by doing:

$(document).ready( function () {
        $('#myTable').DataTable({
                processing: true,
                serverSide: false,
                ajax: '/consultant/timesheets/workdays/'+parseInt(window.location.href.split('/')[6].replace('#!', '')),
                order: ([3, 'des']),
                columns: [
                    { data: 'timesheet_id', name: 'timesheet_id', visible: false},
                    { data: 'id', name: 'id', visible: false },
                    { data: 'day', name: 'day', orderable: false, visible: true},
                    { data: 'date_sort', name: 'date_sort', visible: false},
                    { data: 'date', name: 'date', orderData: [ 3 ] },
                    { data: 'day_type', name: 'day_type' },
                    { data: 'client', name: 'client' , visible: true},
                    { data: 'opportunity', name: 'opportunity' , visible: true},
                    { data: 'start', name: 'start' },
                    { data: 'end', name: 'end' },
                    { data: 'duration', name: 'duration', visible: false },
                    { data: 'pause', name: 'pause' },
                    //{ data: 'comment', name: 'comment', orderable: false},
                    { data: 'total', name: 'total'},
                    // { data: 'confirm', name: 'confirm'},
                    { data: 'actionButtons', name: 'actionButtons'},
                    //{ data: 'updated_at', name: 'updated_at'},
                ],
            });
        });

It grabs the URL and gets the ID from it to pass it to the ajax route. And then I can pass the ID to the controller and use it in the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文