2016-06-20 132 views
0

嗨,我正在做一個與Ajax的CRUD,我有一個商店評論submited問題。 我有這樣的錯誤:Laravel 5.1 - Crud with ajax

QueryException in Connection.php line 655: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into comments (content , user_id , product_id , article_id , updated_at , created_at) values (my comment, , , , 2016-06-20 10:37:57, 2016-06-20 10:37:57))

我tryed存儲與文本「我的評論」評論。我的輸入「內容」傳遞給我的控制器,但隱藏像「article_id」,「user_id」,「product_id」的輸入沒有傳遞給我的控制器。

CommentController:

public function store(Request $request) 
    { 

     if($request->ajax()){ 
      $comment = new Comment(); 
      $comment->content = $request->input('content'); 
      $comment->user_id = $request->input('user_id'); 
      $comment->product_id = $request->input('product_id'); 
      $comment->article_id = $request->input('article_id'); 

      $comment->save(); 


      return response()->json([ 

       "message" => "Comment pubblished!" 
      ]); 
     } 
} 

表評論文章:

{!! Form::open(['route'=>'comment.store'])!!} 



           <div class="form-group"> 
            <label for="reply-text" class="sr-only">Commenta</label> 


            {!! Form::textarea('content', null, ['id'=>'content','class'=>'form-control','rows'=>'3', 'placeholder'=>'Commenta','required'])!!} 


           </div> 
           <input type="hidden" id="token" name="_token" value="{{ csrf_token() }}"> 
           {!! Form::hidden('user_id', Auth::user()->id, null,['id'=>'user_id','class' =>'form-control'])!!} 

           {!! Form::hidden('article_id', $article->id, null,['id'=>'article_id','class' =>'form-control'])!!} 

           {!! Form::hidden('article_slug', $article->slug, null,['id'=>'article_slug','class' =>'form-control'])!!} 

           {!!link_to('#', $title='Comment post', $attributes =['id'=>'commento', 'class'=>'btn btn-lg btn-dark btn-outline'], $secure = null)!!} 

{!! Form::close()!!} 

comment.js:

$("#commento").click(function(){ 

    var dato= $("#content").val(); 
    var dato2= $("#user_id").val(); 
    var dato3= $("#article_id").val(); 
    var dato4= $("#product_id").val(); 
    var route = "http://localhost:8000/comment"; 
    var token = $("#token").val(); 

    $.ajax({ 
     url: route, 
     headers:{'X-CSRF-TOKEN':token}, 
     type: 'POST', 
     dataType: 'json', 
     data:{ 
      content: dato, 
      user_id: dato2, 
      article_id: dato3, 
      product_id: dato4 
     }, 


    }); 

}); 
+0

您可以嘗試更改類型的內容從textarea隱藏起來,看看會發生什麼?它是否會傳遞給你的控制器? –

+0

如果我更改爲隱藏,我可以看到我的文本是寫一個提交,是評論文本傳遞給我的控制器,但隱藏其他輸入像user_id沒有通過。正如你可以看到的錯誤,也許是一個問題comment.js –

+0

當你警報(dato2)時你會得到什麼;在點擊事件? – Frisbetarian

回答

0

更改隱藏字段:

{! Form :: hidden('user_id',Auth :: user() - > id,['id'=>'user_id','class'=>'form-control'])}} ! Form :: hidden('article_id',$ article-> id,['id'=>'article_id','class'=>'form-control'])!!}

{! Form :: hidden('article_slug',$ article-> slug,['id'=>'article_slug','class'=>'form-control'])!!}

+0

您向Form :: hidden的參數添加了一個額外的空值 {{Form :: hidden (名稱,值,屬性數組)}} –

+0

太棒了!!歡迎您:) –