laravel UrlGenerationException(缺少 [Route] 所需的参数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38177872/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:05:38  来源:igfitidea点击:

UrlGenerationException (Missing required parameters for [Route])

phplaravellaravel-5.2

提问by Francisunoxx

I don't know where this error came from it says. I got this error when I tried to update my records. It works properly and updating into my database but getting this error. It says that that missed something in my parameter. I think my error is return redirect()->route('account.show');when I tried to redirect to my showview after updating. But when I redirect to other works fine. Any opinions?

我不知道这个错误来自哪里说。当我尝试更新我的记录时出现此错误。它工作正常并更新到我的数据库中,但出现此错误。它说我的参数中遗漏了一些东西。我认为我的错误是return redirect()->route('account.show');当我尝试在更新后重定向到我的显示视图时。但是当我重定向到其他工作正常时。有什么意见吗?

Missing required parameters for [Route: account.show] [URI: show/{id}].

缺少 [Route: account.show] [URI: show/{id}] 的必需参数。

I have 3 blade template. search, editand show.

我有 3 个刀片模板。搜索编辑显示

search.blade.php - To show all the records.

search.blade.php - 显示所有记录。

@foreach ($result as $row)
    <tr class = "success">
        <td>{{ $row->id }}</td>
        <td>{{ $row->first_name }}</td>
        <td>{{ $row->last_name }}</td>
        <td>{{ $row->middle_name }}</td>
        <td>{{ $row->email }}</td>
        <td>{{ $row->username }}</td>
        <td>
            <a href = "{{ route ('account.show', $row->id) }}"><button type = "submit" class = "btn btn-info">View</button></a>

            <a href = "{{ route ('account.edit', $row->id) }}"><button type = "submit" class = "btn btn-warning">Edit</button></a>

            <a href = "{{ route ('result.destroyEmployee', $row->id) }}"><button type = "submit" class = "btn btn-danger">Archive</button></a>
        </td>
    </tr>
@endforeach

edit.blade.php - Here where I got the error after hitting the submit button which is Save

edit.blade.php - 点击提交按钮保存后出现错误的地方

<div class = "col-md-6">
<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('account.edit', $result->id) }}">

<h3>Edit Employee</h3>
<hr>

<div class = "form-group">

    <label for = "email" class = "control-label">Email Address</label>
    <input type = "text" name = "email" class = "form-control" value = "{{ $result->email }}">

</div>

<div class = "form-group">

    <label for = "username" class = "control-label">Username</label>
    <input type = "text" name = "username" class = "form-control" value = "{{ $result->username }}">

</div>

<div class = "form-group">

    <button type = "submit" class = "btn btn-success">Save</button>

</div>

<input type = "hidden" name = "_token" value = "{{ Session::token() }}">

</form>

show.blade.php - Textfield's will just place the value of the records.

show.blade.php - 文本字段只会放置记录的值。

<div class = "col-md-6">
<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('account.show', $result->id) }}">

<h3>View Employee</h3>
<hr>

<div class = "form-group">

    <label for = "email" class = "control-label">Email Address</label>
    <input type = "text" name = "email" class = "form-control" placeholder = "{{ $result->email }}" readonly>

</div>

<div class = "form-group">

    <label for = "username" class = "control-label">Username</label>
    <input type = "text" name = "username" class = "form-control" placeholder = "{{ $result->username }}" readonly>

</div>

<input type = "hidden" name = "_token" value = "{{ Session::token() }}">

</form>

Routes:

路线:

//READ
Route::get('/search',
[
    'uses' => '\App\Http\Controllers\AccountController@getEmployee',
    'as' => 'account.search',
]);


//SHOW
Route::get('/show/{id}',
[
    'uses' => 'AccountController@showEmployee',
    'as' => 'account.show',
]);

//EDIT
Route::get('/edit/{id}',
[
    'uses' => '\App\Http\Controllers\AccountController@editEmployee',
    'as' => 'account.edit',
]);


//UPDATE
Route::post('/edit/{id}',
[
    'uses' => '\App\Http\Controllers\AccountController@updateEmployee',
    'as' => 'account.edit',
]);

回答by Panagiotis Koursaris

Try to change this:

尝试改变这一点:

<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('account.show', $result->id) }}">

To:

到:

 <form class = "form-vertical" role = "form" action="{{ url('edit/'. $result->id) }}" method="POST">

Then you have to pass the route parameters as second argument to route. Something like this: return redirect()->route('account.show', [$id])

然后你必须将路由参数作为第二个参数传递给路由。像这样的东西:return redirect()->route('account.show', [$id])

*I would recommend you to use laravel collective form: https://laravelcollective.com/

*我建议您使用 laravel 集体形式:https://laravelcollective.com/

With laravel collective you can do something like this:

使用 laravel 集体你可以做这样的事情:

{!! Form::open(array('route' => array('account.show', $result->id))) !!}