对于与问题相关的注释,我有一个morphMany关系模型我想知道如何在保存新注释模型时获取新插入的ID。Laravel从保存的关系中获取新插入的ID
public function postComment() {
if(Request::ajax() && Auth::check()) {
//Input::merge(array_map('trim', Input::all()));
$comment = new Comment;
$comment->user_id = Auth::user()->id;
$comment->body = Helper::strip_tags(Input::get('body'));
$question_id = Input::get('question_id');
$question = Question::find($question_id);
// here in the if statement how do I get the newly created id of a comment
if($question->comments()->save($comment)) {
return Response::json(array('success' => true, 'body' => Input::get('body'),
'userlink' => HTML::linkRoute('profile', Auth::user()->username, array('id' => Auth::user()->id)), 'date' => date("F j, Y, g:i a")));
} else {
return Response::json(array('success' => false, 'body' => Input::get('body')));
}
}
}
是的,我试过了,当我把它放在我的Response :: json – ONYX
中时,它给了我未定义的内容,我只是现在就测试了它并按预期工作。你的评论模型中是否有主键?您也可以尝试'$ comment-> getKey()'来检索模型主键。 –