laravel中缺少必需参数

时间:2018-06-17 08:03:23

标签: php laravel-5 laravel-blade

我一直试图将这个问题解决好几天了。我不知道自己做错了什么。我正在尝试编辑信息中心中的帖子。 我的路线看起来像这样

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

1 个答案:

答案 0 :(得分:0)

解决方案1:主键应该是id而不是post表中的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));
}