所以我有一个帖子模型,有很多评论和属于一个用户,所以当我想添加一个评论,其中属于一个职位和用户,我必须给它一个用户一个ID,这就是我的尝试。Laravel CommentsController没有足够的参数传递user_id
use App\Posts;
use App\Comment;
class CommentsController extends Controller
{
public function store(Posts $post)
{
$this->validate(request(), ['body' => 'required|min:2']);
$post->addComment(request([
'body' => request('body'),
'user_id' => auth()->user()]));
}
}
但是我所得到的是
Type error: Too few arguments to function App\Posts::addComment(), 1 passed 2 expected.
的addcoment方法,从岗位模型:
public function addComment($body, User $userid)
{
$this->comments()->create(compact('body', 'userid'));
return back();
}
按照本教程https://laracasts.com/series/laravel-from-scratch-2017/episodes/19,但导师跳过此步骤。
显示您的路由 –
我将它添加到帖子中。 –
看起来,你只是传递一个参数到'addComment'函数,而它需要两个。 'request'('body'=> request('body'), 'user_id'=> auth() - > user()])' –