将参数传递给Route:缺少[Route:post]错误所需的参数

时间:2019-01-01 16:25:25

标签: php laravel routes

我正在尝试将参数id从我的视图传递到路线。我认为blog.blade.php,我做了以下工作:

<a href="{{ route('post', ['id' => $post->id ] ) }}" class="btn btn-primary">Read More </a>    

我的路线如下:

Route::get('blog/post/{id}', [
  'uses' =>  'PostController@getPost',
  'as' => 'post'
]);

我的控制器动作如下:

public function getPost( $id)
{
    $post = Post::where('id','=',$id)->with('likes')->with('user')->first();
    return view('section.blog', ['post' => $post]);
}

但是每当我加载页面时,都会出现以下错误

  

缺少[Route:post] [URI:blog / post / {id}]所需的参数。 (视图:D:\ Web Development \ ACES \ resources \ views \ section \ blog.blade.php)

我通过在视图上显示$post->id的值是否存在并存在

来检查

更多
我尝试从路线中删除{id}部分,使其像

Route::get('blog/post/', [
  'uses' =>  'PostController@getPost',
  'as' => 'post'
]);

页面已加载,但链接看起来像:

http://localhost/blog/post?id=2

然后我在Controller中出错

为什么会这样?为什么参数没有传递到路由?
我正在使用Laravel 5.7

更新

php artisan route:list看起来像这样

    +--------+----------+---------------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method   | URI                             | Name             | Action                                                                 | Middleware   |
+--------+----------+---------------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD | /                               | home             | Closure                                                                | web          |
|        | GET|HEAD | about-us                        | about            | Closure                                                                | web          |
|        | GET|HEAD | aces-admin                      | admin.home       | Closure                                                                | web          |
|        | GET|HEAD | aces-admin/all-posts            | admin.allposts   | Closure                                                                | web          |
|        | GET|HEAD | aces-admin/all-users            | admin.allusers   | Closure                                                                | web          |
|        | GET|HEAD | aces-admin/edit-events          | admin.events     | Closure                                                                | web          |
|        | GET|HEAD | aces-admin/edit-messages        | admin.messages   | Closure                                                                | web          |
|        | GET|HEAD | aces-admin/edit-study-materials | admin.study      | Closure                                                                | web          |
|        | GET|HEAD | aces-admin/manage-photos        | admin.photos     | Closure                                                                | web          |
|        | GET|HEAD | api/user                        |                  | Closure                                                                | api,auth:api |
|        | GET|HEAD | blog                            | blog             | App\Http\Controllers\PostController@getIndex                           | web          |
|        | GET|HEAD | blog/create                     | create           | Closure                                                                | web          |
|        | GET|HEAD | blog/post/{id}                  | post             | App\Http\Controllers\PostController@getPost                            | web          |
|        | GET|HEAD | events                          | event            | Closure                                                                | web          |
|        | GET|HEAD | gallery                         | gallery          | Closure                                                                | web          |
|        | GET|HEAD | login                           | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST     | login                           |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | POST     | logout                          | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | POST     | password/email                  | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
|        | GET|HEAD | password/reset                  | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
|        | POST     | password/reset                  | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
|        | GET|HEAD | password/reset/{token}          | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | GET|HEAD | register                        | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
|        | POST     | register                        |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
+--------+----------+---------------------------------+------------------+------------------------------------------------------------------------+--------------+

3 个答案:

答案 0 :(得分:1)

尝试第二种方法并按如下所示更新控制器。

public function getPost(Request $request)
{
    $id = $request->get('id');
    $post = Post::where('id','=',$id)->with('likes')->with('user')->first();
    return view('section.blog', ['post' => $post]);
}

答案 1 :(得分:1)

要查看您的信息,您无需在定位标记中使用路线。试试这个:

<a href="/blog/post{{ $post->id }}" class="btn btn-primary">Read More </a> 

如果您正确设置了控制器,Laravel将自动获取ID。

答案 2 :(得分:0)

以这种形式定义您的路线:

Route::get('blog/post/{id}', 'PostController@getPost')->name('post');