Laravel 中缺少路由所需的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41840187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing required parameters for route in Laravel
提问by steven
in my laravel application whenever i go to this route: /admin or admin/users I get the following error message but the the other routes work like admin/users/create or admin/users/edit:
在我的 Laravel 应用程序中,每当我转到此路由时:/admin 或 admin/users 我收到以下错误消息,但其他路由的工作方式类似于 admin/users/create 或 admin/users/edit:
Missing required parameters for [Route: admin.users.edit] [URI: admin/users/{id}/edit]. (View: C:\xampp\htdocs\CMS\resources\views\admin\index.blade.php)
To note I'm not including all the html code of index.blade.php as it's too long the only blade code is from what i have pasted.
请注意,我没有包含 index.blade.php 的所有 html 代码,因为它太长了,唯一的刀片代码来自我粘贴的内容。
Index.blade view:
Index.blade 视图:
<ul class="nav nav-second-level">
<li>
<a href="{{route('admin.users.edit')}}">All Userss</a>
</li>
<li>
<a href="{{route('admin.users.create')}}">Create User</a>
</li>
</ul>
Route list:
路线清单:
URI:
网址:
- admin/users
- admin/users/create
- admin/users/store
- admin/users/update
- admin/users/{id}/edit
Name:
姓名:
1. admin.users.index
2. admin.users.create
3. admin.users.store
4. admin.users.update
5. admin.users.edit
Action:
行动:
1. App\Http\Controllers\AdminUsersController@index
2. App\Http\Controllers\AdminUsersController@create
3. App\Http\Controllers\AdminUsersController@store
4. App\Http\Controllers\AdminUsersController@update
5. App\Http\Controllers\AdminUsersController@edit
admin/users
管理员/用户
Routes:
路线:
Route::get('admin/users/', [
'uses' => 'AdminUsersController@index',
'as' => 'admin.users.index'
]);
Route::get('admin/users/create', [
'uses' => 'AdminUsersController@create',
'as' => 'admin.users.create'
]);
Route::get('admin/users/{id}/edit', [
'uses' => 'AdminUsersController@edit',
'as' => 'admin.users.edit'
]);
Controller:
控制器:
public function edit($id)
{
$user = User::findOrFail($id);
$roles = Role::pluck('name', 'id')->all();
return view('admin.users.edit', compact('user', 'roles'));
}
public function update(Request $request, $id)
{
//
}
Edit View:
编辑视图:
@extends('layouts.admin')
@section('content')
<h1>Edit Users</h1>
{!! Form::open(['method' => 'PATCH', 'action' => ['AdminUsersController@update', $user->id], 'files' => true]) !!}
@include('includes.form-error')
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email:') !!}
{!! Form::email('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
<div class="form-group">
{!! Form::label('role_id', 'Role:') !!}
{!! Form::select('role_id',$roles , null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('status', 'Status:') !!}
{!! Form::select('status', [1 => 'Active', 0 => 'Not Active'], 0, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('photo_id', 'Photo:') !!}
{!! Form::file('photo_id', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password:') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Create User', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
@endsection
回答by Rashad
Here is your error. You missed to pass the variable in route:
这是你的错误。您错过了在路由中传递变量:
<li>
<a href="{{route('admin.users.edit')}}">All Users</a>
</li>
Instead of {{route('admin.users.edit')}}
use:
而不是{{route('admin.users.edit')}}
使用:
{{route('admin.users.edit', $id)}}
{{route('admin.users.edit', $id)}}
This is actually editing route. If you want to get all users use:
这实际上是编辑路线。如果您想让所有用户使用:
{{route('admin.users.index')}}
{{route('admin.users.index')}}
回答by Eimantas Gabrielius
Change {{route('admin.users.edit')}}
to {{route('admin.users.edit', $id)}}
and dont forget to change $id
to the real id variable :)
更改{{route('admin.users.edit')}}
为{{route('admin.users.edit', $id)}}
并且不要忘记更改$id
为真实的 id 变量:)
And its strange, because you write All users
and giving the edit route :)
这很奇怪,因为您编写All users
并提供了编辑路线:)
回答by Marcin Nabia?ek
You are using:
您正在使用:
<a href="{{route('admin.users.edit')}}">All Userss</a>
in your Blade file, but your route is defined like so:
在您的 Blade 文件中,但您的路线是这样定义的:
Route::get('admin/users/{id}/edit', [
'uses' => 'AdminUsersController@edit',
'as' => 'admin.users.edit'
]);
so it's edit action and you need to pas id of user you want to edit for example:
所以它是编辑操作,您需要传递要编辑的用户 ID,例如:
<a href="{{route('admin.users.edit', [auth()->user()->id])}}">All Userss</a>