2014-06-09 35 views
0

我正在嘗試爲博客上的帖子創建評論系統。我創建了一個上傳評論的表單。表格中的user_id和post_id列都已正確填充,但輸入文本並提交時,正文列是空的。我定義了一個關係,一個職位有許多評論。另外,我定義了一個用戶有很多評論。這裏是我的代碼: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

謝謝,文本現在上傳到數據庫。但由於某種原因,它將每個post_id設置爲2.爲什麼會這樣呢? – codeforfood

相關問題