php - 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
解决方案
解决方案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));
}
推荐阅读
- linux - 如何通过门户实时跟踪终端的进度?
- javascript - 从javascript输入提示框中返回一个值到C#
- maya - Maya 脚本是否区分大小写?
- aiosmtpd - 接收电子邮件时通过 aiosmtpd 断开连接
- excel - 如何遍历一列并检查该值是否与下一个值匹配然后追加?
- python - 使用 Python3 运行 Excel 宏的最佳方法是什么?
- google-chrome-extension - 更改 chrome.identity.getAuthToken 的 Google 帐户
- angular - 错误:找不到模块'@angular/compiler-cli/linker'
- dynamics-365 - 从 D365 ONLINE 插件创建 Sharepoint ONLINE 文件夹
- language-agnostic - 很难选择合适的变量名