2017-12-04 13 views
0

投稿メソッドを使用してフォームを送信しようとしていますが、クエリ例外エラーが発生しています。laravelでajaxを使用してフォーム投稿を送信する方法5.4

SQLSTATE [23000]:整合性制約違反:1048列 'post_idの' がヌルにすることはできません(SQL:commentscommentapproveduser_idpost_idupdated_atcreated_at)値(sdfsdfsdff、1、1、挿入、2017年-12-04 07:18:13、2017-12-04 07:18:13))

sdfsdfsdffは「コメントシステム」を送信しようとしているコメントです.1はユーザーID、空白スペースは投稿IDがあるべき場所です。

これは私のコントローラのコードです:

$this->validate($request, [ 
    'comment' => 'required' 
    ]); 
    $post = Post::find($post_id); 
    $comment = new Comment(); 
    $comment->comment = $request->comment; 
    $comment->approved = true; 
    $comment->user_id = auth()->id(); 
    $comment->post_id = $request->id; 
    $comment->save(); 
    return response()->json([ 
    'comment' => $comment->comment, 
    'user_id' => $comment->user_id, 
    'post_id' => $comment->post_id, 
    'post_slug' => $post->slug, 
    'success' => true 
    ]); 

これが私の見解で私のAJAXコードです:

var urlPostComment = '{{ url('comments/store') }}'; 

$('.send').click(function(e){ 
    e.preventDefault(); 
    var dataSerialize = $('#comment-new').serialize(); 

    $.ajax({ 
     method: 'post', 
     url: urlPostComment, 
     data: dataSerialize, 
     dataType: 'json', // <- La coma 
     success: function (data) { 
     console.log(data); // <- Revisar JSON en la consola del navegador 
     if(data.success) 
     { 
      $('#comment-post').append(
       '<p class="store-comment">' + data.comentario + '</p>' 
      ); 
     } 

     }, // <- La coma 
     error: function() { 
     console.log('Error en el AJAX'); 
     }, // <- La coma 
     complete: function() { 
     console.log('Termino el ajax'); 
     } 
    }); 
}); 

マイフォーム:

<div class="row"> 
      <div class="col-xs-8 col-md-offset-2 form-create"> 

      {!! Form::open(['route' => ['comments.store', $post->id], 'method' => 'POST', 'id' => 'add-comment']) !!} 
       <p class="error text-center alert alert-danger hidden"></p> 
        {{ Form::label('comment', ' Comentario', ['class' => 'glyphicon glyphicon-comment pull-right label-space', 'id' => 'comment-create']) }} 
        {{ Form::textarea('comment', null, ['class' => 'form-control comment-text', 'id' => 'comment-new', 'placeholder' => 'Escribe tu comentario']) }} 

        <p class="error text-center alert alert-danger hidden"></p> 

        {{ Form::submit('Agregar Comentario', ['class' => 'comment-message-guest-button send']) }} 

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

      </div> 
      </div> 

、全コメントのhtml:

<div class="row"> 
        <div class="col-md-12"> 

        @foreach ($post->comments as $comment) 
        <section class="comment-list"> 

         <article class="row"> 
         <div class="col-md-3 col-sm-3 hidden-xs"> 
          <figure class="thumbnail"> 
          <img class="img-responsive" src="/uploads/avatars/{{ $comment->user->profilepic }}" /> 
          <figcaption class="text-center">{{ $comment->user->name }}</figcaption> 
          </figure> 
         </div> 
         <div class="col-md-8 col-sm-8"> 
          <div class="panel panel-default arrow left"> 
          <div class="panel-body"> 
           <header class="text-left"> 
           <div class="comment-user"><i class="fa fa-user"></i> {{ $comment->user->name }}</div> 
           <time class="comment-date" datetime="{{ $comment->created_at->diffForHumans() }}"><i class="fa fa-clock-o"></i> {{ $comment->created_at->diffForHumans() }}</time> 
           </header> 
           <div id="comment-post" data-commentid="{{ $comment->id }}"> 
            <p id="display-comment" class="store-comment" {{ $comment->id }} {{ $post->id }}>{{ $comment->comment }}</p> 
           </div> 
          </div> 

          <div class="panel-footer list-inline comment-footer"> 
           @if(Auth::guest()) 

           No puedes responder ningún comentario si no has ingresado. 

           @else 

           @if(Auth::user() == $comment->user) 
           <a href="#" data-toggle="modal" data-target="edit-comment" class="edit-comment">Editar</a> <a href="#" data-toggle="modal" data-target="delete-comment" class="delete-comment">Eliminar</a> 
           @endif 

           @if(Auth::user() != $comment->user) 
           <a href="#">Responder</a> 
           @endif 

           @endif 
          </div> 

          </div> 
         </div> 
         </article> 

        </section> 
        @endforeach 
        </div> 
       </div> 
0私は、私はAJAXを使用していないときに完全に働いた外部キーを持っています POST http://localhost:8000/comments/store 500(内部サーバーエラー)「グーグルクロームコンソールから」

私のコメントテーブルでデータベース上:

メインエラー私はそれが関係問題だとは思わない。事前にありがとう

答えて

0

はい、あなたは正しいです。 これは関係の問題ではありません。しかし、私は推測するハックを持っています。 $ post_idここに$post = Post::find($post_id);があるので、なぜこの行に$ request-> idを使用しますか? $comment->post_id = $request->id;

$ request-> idが投稿IDを持っていないとは思っていません。この

Route::post('comments.store/{id}', ...); 

あなたは

$request->route('id'); 

しかしによって、あなたのリクエストでルートパラメータ{id}にアクセスできるように私はあなたのルートが見えることを前提としてい

$comment->post_id = $post_id; 
+0

私はこの方法を使用する場合、私はこの取得:代わりに、私は「ストア」.. –

0

この方法を使用してみてくださいあなたがコントローラの中にいるので、あなたはのパラメータにアクセスすることができます機能

public function store($id) { 
    echo $id; 
} 
+0

正しいが、私のルートメソッドは 'コードルート::ポスト(「コメント/ {post_idの}」であることをGET、POST ID番号の店を、['uses' => 'CommentsController @ store'、 'as' => 'コメント。店舗 ']); 'とAjaxを使ってコメントを編集することは完璧に動作した場合、実際に私が –

+0

すぎスラグを持って、私はコンソールでこれを取得する:'コードのコメントを:41 _token:kRNLLCBMlsnueKdbkvMmBC56xssXHhfEyRPy3L50 _method:PUT' –

1

あなたのフォームIDである「-コメントを追加するには」が、あなたのAJAXであなたは「コメント-新しい」を通して、あなたのフォームをシリアル化しています。 あなたのコントローラに入っているフォームにname = "id"フィールドを定義していません。

$comment->post_id = $request->id; 
+0

このコメント commentIdを編集しますはい、私は "コメントを追加"をシリアル化するので、私は編集コメントをクリックすると、新しいコメントフィールドは古いコメント値を取ることを認識している..すでに$ request-> idとtryedと働いていない –