2016-02-25 98 views
0

我想使用表单检索用户评论的数据,但我不太确定如何执行此操作,即使在查看在laravel文档和视频。如何从表单中检索数据(type =“text”)并将其存储在数据库中laravel 5.2

我 “CommentsController” 代码snipit看起来像这样

public function submitComment(Request $request){ 

     $this->validate($request, [ 
      'comment'=> 'required|max:500' 
     ]); 

     $comment= new Comments; 
     $comment->comments=$request->input->('comment'); 


     DB::table('comments')->insert(
    array('user_id'=>1, 
     'post_id'=>1, 
     'comment'=> $comment) 
); 

,我的形式snipit看起来像这样

<?php 


     echo '<form method="POST" action="comments"> '; 
     echo '<input name="comment" type="text" cols="40" rows="5" style="width:200px; height:50px;" placeholder="Type Here">'; 
     echo '<input type="submit">' ; 
     echo '</form>'; 



     ?> 

和我的数据库中的表看起来像这样

Schema::create('comments', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->integer('user_id')->unsigned(); 
    $table->integer('post_id')->unsigned(); 
    $table->string('comments'); 
    $table->timestamps(); 

    //Foreign Keys 
    $table->foreign('post_id') 
     ->references('id')->on('posts') 
     ->onDelete('cascade'); 
    $table->foreign('user_id') 
     ->references('id')->on('users') 
     ->onDelete('cascade'); 

}); 

回答

0

尝试改变

$comment->comments=$request->input->('comment'); 

$comment->comments =$request->comment; 

基本上可以通过$请求 - >得到输入表格名称

我也帐篷像

$record = Comment::create([ 'user_id' => $userId, 'post_id' => $postId, 'comments' => $request->comments]); 

创造新的记录,但你需要创建一个模型来做到这一点。

php artisan make:model Comment 
相关问题