2013-03-16 4 views

답변

1

사용자 ID를 배열에 명시 적으로 지정할 수 있습니다.

$post->comments()->create(array(
    'body' => 'Comment message', 
    'user_id' => Auth::user()->id 
)); 

또는 주석을 작성한 다음 posts 및 users 테이블과 함께 관계를 삽입 할 수 있습니다.

$post = Post::find($whatever); 
$user = Auth::user()->id; 

$comment = Comment::create(array(
    'body' => 'Comment message' 
)); 

$post->comments()->insert($comment); 
$user->comments()->insert($comment); 

Laravel 4에서는 마지막 두 행에 대한 save($comment) 대신 insert($comment) 사용할 수 있습니다. 이것은 Laravel 3의 주요 변경 사항입니다.

+0

나는 이런 종류의 작업을 통해 with() –

+0

여기 까다로운 점은 댓글이 게시물과 사용자 모두에 속한 것입니다. 하나의 관계를 삽입하는 것은 간단하지만 두 가지 방법을 사용하는 더 짧고/깨끗한 방법은 없다고 생각합니다. – bilalq

+0

다른 옵션으로 답변을 업데이트했습니다. – bilalq