首页 > 解决方案 > laravel 中缺少必需的参数

问题描述

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

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

标签: phplaravel-5laravel-blade

解决方案


解决方案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));
}

推荐阅读