简体   繁体   中英

Missing required parameter in laravel

I've been trying to get this fixed for days now. I don't know what i'm doing wrong. I'm trying to edit a post in the dashboard. My route looks like this

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

for that route i have a controller

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));
  }

My blade syntax looks like this

  @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

Solution 1: primary key should be id instead of post_id in post table.

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));
   }

Solution 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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM