[Route: ] - Laravel 7 缺少必需的参数

Posted

技术标签:

【中文标题】[Route: ] - Laravel 7 缺少必需的参数【英文标题】:Missing required parameters for [Route: ] - Laravel 7 【发布时间】:2021-11-15 22:06:31 【问题描述】:

我在 laravel 5 的生产服务器上没有收到此错误。我进行了版本升级,但在收到此错误后。我真的不知道版本升级是否导致了这种情况。但是我在 laravel 7 中遇到了这个错误。

错误 - 缺少 [Route: ] [URI: users/user_id/privacy-groups-modal/list_id] [缺少参数:user_id, list_id] 的必需参数。 (查看:/opt/lampp/htdocs/php/resources/views/wishlist/show.blade.php)

Web.php

Route::group(['middleware' => ['imageOptimizer', 'auth']], function() 
Route::get('users/user_id/privacy-groups-modal/list_id', 'UserController@getPrivacyGroupsModel');
);

show.blade.php

引用评论

//Error Line: 
var link = 'action("UserController@getPrivacyGroupsModel", [\Auth::id(), "list_id"])'; 
//Error line
    $(document).on('changed.bs.select', '.privacy-selector', function (e, clickedIndex, newValue, oldValue) 
    
                if (!triggeredByClick)
                    changeTriggered = true;
                var _self = $(this);
                var val = _self.selectpicker('val');
                var prevValue = oldValue;
    
                if ($.isNumeric(val)) 
                    var privacy_level = _self.find('option[value='+val+']').attr('data-type');
                    if (privacy_level == 'custom') 
                        var link = 'action("UserController@getPrivacyGroupsModel", [\Auth::id(), "list_id"])'; //Error line
                        link = link.replace('list_id', _self.attr('data-list-id'));
    
                        $.ajax(
                            url: link
                        )
                                .done(function(response) 
                                    privacy_group_modal = bootbox.dialog(
                                        title: '@lang("app.my_privacy_groups")',
                                        message: response,
                                        backdrop: false,
                                        animate: false,
                                        buttons: 
                                            cancel: 
                                                label: '@lang("app.cancel")',
                                                className: 'btn-default',
                                                callback: function () 
                                                    privacy_group_modal = null;
                                                    bootbox.hideAll();
                                                    _self.selectpicker('val', prevValue);
                                                    _self.selectpicker('refresh');
                                                
                                            
                                        
                                    );
                                    $('input[type=hidden][name=list_id]').val(_self.attr('data-list-id'));
                                )
                                .fail(function() 
                                    $.toast(
                                        heading: 'Error',
                                        text: '@lang("app.error_occurred")',
                                        showHideTransition: 'slide',
                                        hideAfter: 2000,
                                        position: 'top-center',
                                        icon: 'error',
                                        afterHidden: function () 
                                            _self.selectpicker('val', prevValue);
                                            _self.selectpicker('refresh');
                                        
                                    );
                                );
    
                     else 
                        var link = 'action("WishlistController@updateListPrivacyLevel", ["list_id", "level_id"])';
                        link = link.replace('list_id', _self.attr('data-list-id'));
                        link = link.replace('level_id', val);
    
                        $.ajax(
                            url: link,
                        )
                                .done(function() 
                                    $.toast(
                                        heading: 'Success',
                                        text: '@lang("app.successfully_updated")',
                                        showHideTransition: 'slide',
                                        hideAfter: 5000,
                                        position: 'top-center',
                                        icon: 'success'
                                    );
                                )
                                .fail(function() 
                                    $.toast(
                                        heading: 'Error',
                                        text: '@lang("app.error_occurred")',
                                        showHideTransition: 'slide',
                                        hideAfter: 2000,
                                        position: 'top-center',
                                        icon: 'error',
                                        afterHidden: function () 
                                            _self.selectpicker('val', prevValue);
                                            _self.selectpicker('refresh');
                                        
                                    );
                                );
                    
                
    
                if (triggeredByClick)
                    triggeredByClick = false
            );

UserController.php

public function getPrivacyGroupsModel($user_id, $list_id)
    
        $validator = Validator::make([
                'user_id' => $user_id,
                'list_id' => $list_id
            ], [
                'user_id' => 'required|exists:users,id',
                'list_id' => 'required|exists:wishlists,id',
            ]);

        if ($validator->fails()) 
            return response()->json($validator->errors()->all(), 422);
        

        $privacy_level_custom = $this->privacy_level_repository
            ->getPrivacyLevelByName('Custom');

        $privacy_groups_of_user = $this->privacy_group_repository
            ->getPrivacyGroupsByUserId((int) $user_id);

        $wishlist = $this->wishlist_repository
            ->getWishlistById((int) $list_id);
        return view()->make('user.partials._privacy-groups-modal', [
                'wishlist' => $wishlist,
                'level_id' => $privacy_level_custom->id,
                'share_with_groups' => $privacy_groups_of_user->where('type', PrivacyGroup::TYPE_SHARE_WITH),
                'unshare_with_groups' => $privacy_groups_of_user->where('type', PrivacyGroup::TYPE_DONT_SHARE_WITH)
            ])
            ->render();
    

这是升级后 JQuery 语法的问题吗?非常感谢您的帮助

【问题讨论】:

【参考方案1】:

你必须像下面这样使用

var authID = "\Auth::id()";

var link   = " url('') /users/"+authID+"/privacy-groups-modal/list_id";

link       = link.replace("list_id", _self.attr('data-list-id'));

【讨论】:

【参考方案2】:

你也可以使用

Route::get('users/user_id/privacy-groups-modal/list_id','UserController@getPrivacyGroupsModel')->name('user-privacy');


var link = 'route("user-privacy", [\Auth::id(), "list_id"])'; 

【讨论】:

以上是关于[Route: ] - Laravel 7 缺少必需的参数的主要内容,如果未能解决你的问题,请参考以下文章

从 xampp 移动到 apache 2.4.7 后,Laravel 路由返回 404。 mod_rewrite aur htaccess 或缺少 apache 设置?

域路由错误 Laravel 中缺少必需的参数

Laravel 8:[路线:students.update]缺少必需的参数

Laravel5.7 路由使用 Route:match 不起作用

缺少必需的参数(laravel 8.8.0)

Laravel 7 使用 request->route('id) 分配 foreignID?