【问题标题】:Missing required parameter in laravellaravel 中缺少必需的参数
【发布时间】:2018-06-17 16:49:59
【问题描述】:

我这几天一直在努力解决这个问题。我不知道我做错了什么。我正在尝试在仪表板中编辑帖子。 我的路线是这样的

Route::get('/edit-post/post_id/{post_id}',[
            'as'=>'edit-post',
            'uses'=>'dashboardController@showPostedit',
        ]);

对于那条路线,我有一个控制器

public function showPostEdit(Request $request, Post $post, $post_id){
    $posts= $post->where('post_id',$post_id)->get();

        return view('pages.dashboard.user.edit-post',compact('posts',auth()- 
  >user()->id));
  }

我的刀片语法如下所示

  @if(Auth::check())
    @if(auth()->user()->id === $posts->user_id)
        <li class="list-inline-item"><a href="{{route('edit-post',['user_id'=>auth()->user()->id,'post_id'=>$posts->post_id])}}"><span class="fa fa-edit"></span></a></li>
                        <li class="list-inline-item"><a href="{{route('delete-post',$posts->post_id)}}"><span class="fa fa-trash"></span></a></li>
                    @endif
            @endif

【问题讨论】:

  • 我想在Post $post 中已经有一个必需的帖子,所以可以删除post_id 参数。另外 - 在此处发布错误消息。
  • 此外,您在整个代码中拼写类和方法名称的方式不同,例如dashboardController@showPostedit 与 function showPostEdit()。尽管 PHP 本身是不区分大小写的,但它会出现奇怪的情况......
  • 感谢更改了匹配的类和方法仍然得到相同的错误
  • 已修复,谢谢。

标签: php laravel-5 laravel-blade


【解决方案1】:

解决方案1:post表中的主键应该是id而不是post_id。

Route::get('/edit-post/{post}',[
        'as'=>'edit-post',
        'uses'=>'dashboardController@showPostEdit',
    ]);
public function showPostEdit(Post $post){
    return view('pages.dashboard.user.edit-post',compact('post',auth()->user()->id));
   }

解决方案 2:

Route::get('/edit-post/{postId}',[
        'as'=>'edit-post',
        'uses'=>'dashboardController@showPostEdit',
    ]);
public function showPostEdit($postId){
$post= (new Post())->where('post_id',$postId)->get();
    return view('pages.dashboard.user.edit-post',compact('post',auth()->user()->id));
}

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 2021-07-20
    • 2017-12-27
    • 1970-01-01
    • 2021-11-15
    • 2020-05-07
    相关资源
    最近更新 更多