gpt4 book ai didi

php - Laravel:CRUD 路由有一个空参数

转载 作者:行者123 更新时间:2023-12-05 08:33:21 24 4
gpt4 key购买 nike

我在 CRUD 路由方面遇到问题。在我的 Controller UserGroupsController 中编辑方法时,参数 $userGroup 为空。

用户组 Controller

class UserGroupsController extends Controller
{
public function edit(UserGroup $userGroup)
{
if (Auth::check())
{
$users = User::pluck('name','id')->all();
$groups = Group::pluck('name','id')->all();
return view('users-group.edit', compact('userGroup','users','groups'));
}else{
return redirect('login');
}

}
}

编辑.blade.php

<div class="panel panel-default">
<div class="panel-heading">Edit</div>
<div class="panel-body">
{{ $userGroup }}
{!! Form::model($userGroup,['method' => 'PATCH', 'route' => ['users-group.update', $userGroup->id], 'id' => 'user_group-form', 'class' => 'form-horizontal'] ) !!}
{{ csrf_field() }}

<input type='hidden' name='id' value='{!! $userGroup->id !!}'>
@include('users-group.form',array('submitButton' => 'Edit User Group'))
{!! Form::close() !!}
</div>

表单.blade.php

<div class="form-group">
<label for="user_id" class="col-md-4 control-label">User</label>
<div class="col-md-6">
{!! form::select('user_id', $users, $userGroup->user_id,['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<label for="group_id" class="col-md-4 control-label">Group</label>
<div class="col-md-6">
{!! form::select('group_id', $groups, $userGroup->group_id,['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
{!! Form::submit($submitButton) !!}
</div>
</div>

路线:

Route::group(['middleware'=>['web']], function ()
{
Route::resource('users','UsersController');
Route::resource('users-group','UserGroupsController');
Route::resource('companies','CompaniesController');
});

php artisan 路线:列表:

|        | POST      | users-group                    | users-group.store   | App\Http\Controllers\UserGroupsController@store                        | web          |
| | GET|HEAD | users-group/create | users-group.create | App\Http\Controllers\UserGroupsController@create | web |
| | PUT|PATCH | users-group/{users_group} | users-group.update | App\Http\Controllers\UserGroupsController@update | web |
| | DELETE | users-group/{users_group} | users-group.destroy | App\Http\Controllers\UserGroupsController@destroy | web |
| | GET|HEAD | users-group/{users_group} | users-group.show | App\Http\Controllers\UserGroupsController@show | web |
| | GET|HEAD | users-group/{users_group}/edit | users-group.edit | App\Http\Controllers\UserGroupsController@edit

dd($用户组):

UserGroup {#199 ▼
#fillable: array:2 [▼
0 => "user_id"
1 => "group_id"
]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
+exists: false
+wasRecentlyCreated: false
}

在文件 edit.blade.php 中,我打印变量 $userGroup,但返回“[ ]”。我有与 UsersControllers 相同的代码,但确实有效。

当我提交表单时,出现以下错误:

MethodNotAllowedHttpException in RouteCollection.php

抱歉英文,我希望你能理解我的问题,我可以提供更多信息。谢谢

最佳答案

这是您的代码中的问题。从 php artisan route:list 的输出中可以清楚地看出,UserGroupsController 中的编辑函数所期望的参数名称是 $users_group 而不是 $userGroup 。所以我向您推荐两个简单的解决方案。

解决方案#1像这样更改 UserGroupsController 的编辑功能。

class UserGroupsController extends Controller
{
public function edit(UserGroup $user_group)
{
if (Auth::check())
{
$users = User::pluck('name','id')->all();
$groups = Group::pluck('name','id')->all();
return view('users-group.edit', compact('userGroup','users','groups'));
}else{
return redirect('login');
}

}
}

解决方案#2更改您的路线功能,以便 php artisan route:list 将输出此内容。

|        | POST      | users-group                    | users-group.store   | App\Http\Controllers\UserGroupsController@store                        | web          |
| | GET|HEAD | users-group/create | users-group.create | App\Http\Controllers\UserGroupsController@create | web |
| | PUT|PATCH | users-group/{userGroup} | users-group.update | App\Http\Controllers\UserGroupsController@update | web |
| | DELETE | users-group/{userGroup} | users-group.destroy | App\Http\Controllers\UserGroupsController@destroy | web |
| | GET|HEAD | users-group/{userGroup} | users-group.show | App\Http\Controllers\UserGroupsController@show | web |
| | GET|HEAD | users-group/{userGroup}/edit | users-group.edit | App\Http\Controllers\UserGroupsController@edit

请注意,users_group 必须更改为 userGroup

好的,但为什么?

这是因为 Laravel's Implicit Route Binding . Laravel 自动解析在路由或 Controller 操作中定义的 Eloquent 模型,其类型提示变量名称与路由段名称匹配。例如:

Route::get('api/users/{user}', function (App\User $user) {
return $user->email;
});

因为 $user 变量被类型提示为 App\User Eloquent 模型并且变量名称匹配 {user} URI段,Laravel 将自动注入(inject)具有与请求 URI 中相应值匹配的 ID 的模型实例。如果变量名与 {user} URI 段不匹配,Laravel 将不会注入(inject)任何模型实例,因此该变量将保留为

关于php - Laravel:CRUD 路由有一个空参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41642928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com