【问题标题】:Can't get ID from URL Laravel无法从 URL Laravel 获取 ID
【发布时间】:2021-05-16 18:33:37
【问题描述】:

我有一个显示来自user 的一些帖子的数据表视图,我想创建一个带有可以显示所有用户帖子的按钮的数据表。但是,当从所有帖子列表移动到用户帖子列表时,我无法从 URL 中获取 user_id。 我想使用$request->route 获得user_id。 这是我的控制器:-

 public function index(Request $request)
    {
          if ($request->ajax()) {
            $request->route('detail');
            $data = Post::where('user_id', $this->route('detail'))->latest()->get();
            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('action', function ($row) {

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

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

        return view('detail');
    }

这是我的路线:-

Route::resource('detail', DetailController::class);

数据表代码

var table = $('.data-table').DataTable({
            paging: true,
            info: true,
            autoWidth: false,
            responsive: true,
            
            columnDefs: [{
                    "targets": [0, 2, 3, 4, 5, 6], // your case first column
                    "className": "text-center",

                },

            ],
            language: {
                paginate: {
                    next: '<span class="fas fa-arrow-right"></span>', // or '→'
                    previous: '<span class="fas fa-arrow-left"></span>' // or '←' 
                }
            },
            oLanguage: {
                "oPaginate": {
                    next: '&#8594;', // or '→'
                    previous: '&#8592;' // or '←' 
                },
                "sLengthMenu": "Tampilkan _MENU_ Item",
                "sEmptyTable": "Tidak ada data",
                "sInfoEmpty": "Tidak ditemukan",
                "sLoadingRecords": "Sedang memproses - loading...",
                "sInfo": "Menampilkan _START_ - _END_ dari _TOTAL_ Item",
                "sSearch": "Cari:",
            },
            processing: true,
            serverSide: true,
            ajax: "{{ route('detail.index') }}",

            columns: [{
                    data: 'DT_RowIndex',
                    name: 'DT_RowIndex',
                    orderable: false,
                    searchable: false,
                },
                {
                    data: 'title',
                    name: 'title',
                    orderable: false,
                },
                {
                    data: 'content',
                    name: 'content',
                    orderable: false,
                    visible: false,
                },
                {
                    data: 'progress',
                    name: 'progress'
                },
                {
                    data: 'status',
                    name: 'status'
                },
                {
                    data: 'target_selesai',
                    name: 'target_selesai'
                },
                {
                    data: 'action',
                    name: 'action',
                    orderable: false,
                    searchable: false
                },
            ]
        });

如果我将$this-&gt;route('detail') 更改为 user_id 值,程序将按预期运行。我的代码有问题吗?提前致谢

【问题讨论】:

    标签: laravel laravel-routing laravel-8 laravel-request


    【解决方案1】:

    改变这个:-

    $this->route('detail')
    

    收件人:-

    $request->get('details')
    

    你的ajax方法应该是这样的:

    "ajax": {
        'type': 'POST',
        'url': '{{ route('detail.index') }}',
        'data': {
           detilas: 'your id',
          
        },
    }
    

    你的遭遇:

    Route::post('detail', DetailController::class);
    

    【讨论】:

    • 错误消失,但数据表未显示任何数据。在哪里可以看到我的 ajax 代码?
    • 当你检查if ($request-&gt;ajax()),所以你有ajax代码,它在你的刀片文件中
    • details: 'your id',“你的身份证”是什么?这是数据名还是变量名?
    • 是的,您将id 发送到controller,然后检查您的where 条件
    • 我已经尝试了你的建议,如果我添加 route::match 它会返回 laravel 错误说 App\Http\Controllers\DetailController` is not invokable. 如果我不添加该路由匹配代码,数据表仍然没有显示任何数据
    猜你喜欢
    • 2016-01-20
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2019-02-03
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多