2014-06-09 2 views
0

블로그의 게시물에 대한 의견 시스템을 만들려고합니다. 나는 의견을 업로드하기위한 양식을 만들었습니다. user_id 및 post_id 열 모두 테이블에 제대로 채워지지만 텍스트를 입력하고 제출할 때 본문 열은 비어 있습니다. 나는 하나의 포스트가 가진 관계를 정의했다. 많은 코멘트. 또한, 나는 하나의 사용자 hasMany Comments를 정의했다. 여기 내 코드입니다 :Laravel 양식 텍스트가 데이터베이스에 업로드되지 않음

//commentcontroller.php 

public function newComment(){ 

     $id=Auth::id(); 

     $comment = New Comment(); 

     $comment->user_id=$id; 
     $comment->post_id=Input::get('post_id'); 
     $comment->body=Input::get('body'); 

     post->comments()->save($comment); 
} 

형태 :

<div class="col-md-8 col-md-offset-2"> 

<hr> 

@foreach($posts as $post) 
    <h3>{{ $post->title}}</h3> 
    <p>by {{ $post->user->name }}</p> 
    <img src='{{ asset($post->image_path) }}' class="img-responsive" id="blogpic"/> 
    <p>{{ Str::words($post->body) }}</p> 
    <p><a href="{{ action('[email protected]', $post->id) }}">Read More...</a></p> 

    <hr> 
      //COMMENT AREA 
    {{ Form::open(array('action' => '[email protected]', 'files'=>true)) }} 
      {{ Form::textarea('body', null, array('class'=>'form-control')) }} 
    {{ Form::hidden('post_id', $post->id) }} 

    <br> 

    <br> 


    {{ Form::submit('Post') }} 

@endforeach 

</div> 

내가 표시하는 의견에 대해 아직 걱정하고 있지 않다, 단지 데이터베이스에 업로드. user_id와 post_id가 데이터베이스에 올바르게 업로드되면 텍스트는 저장되지 않습니다. 미리 감사드립니다.

답변

2

이 작업을 시도 할 수 있습니다 :

public function newComment() 
{ 
    $comment = New Comment(); 
    $comment->user_id = Auth::user()->id; 
    $comment->post_id = $post_id = Input::get('post_id'); 
    $comment->body = Input::get('body'); 

    Post::find($post_id)->comments()->save($comment); 
} 

공지 사항 Auth::user()->id 대신 Auth::id() 사용됩니다.

+0

답장을 보내 주셔서 감사합니다.하지만 Laravel은 post-> comments() -> save ($ comment) 구문 분석 오류를 발생 시켰습니다. 선. 무엇이 잘못 될 수 있습니까? – codeforfood

+0

업데이트 된 답변을 확인하십시오. 통지하지 않았습니다. –

+0

감사합니다. 이제 텍스트가 DB에 업로드됩니다. 그러나 어떤 이유로 든 모든 post_id를 2로 설정합니다. 그 이유는 무엇입니까? – codeforfood

관련 문제