lundi 20 juin 2016

Laravel 5.1 - Crud with ajax

Hi i'm doing a CRUD with ajax, i have a problem with store comment submited. I have this error:

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))

i tryed to store a comment with text "my comment". My input "content" pass to my controller but my input hidden like "article_id", "user_id", "product_id" are not passing to my controller.

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 Comment article:

{!! 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="">
                                {!! 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
        },


    });

});



via Chebli Mohamed

1 commentaire:

Onpeaks a dit…

Hi, ExpressTech Softwares laravel Development Company provides fluency in delivering simple code for a variety of bundles to help create effective and Secure websites for Various fields of industry and domains. We help businesses to achieve their estimated goals using leading solutions from the Laravel framework. Contact@ExpressTechSoftwares.Com or +91-9806724185

Enregistrer un commentaire