1
votes

I'm using laravel 5.5 and im trying to add a comment to a post and i get the following error when i submit to the form

"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null (SQL: insert into comments (comment_body, user_id, post_id, updated_at, created_at) values (sdsd, 1, , 2017-12-03 12:29:58, 2017-12-03 12:29:58))

im going to be using: <% %> is for angular, just letting everyone know.

In tinker this works

 Comment::create(['comment_body' => 'this works', 'user_id'=> 1, 'post_id'=>8]);

**Route*

Route::post('post/comment', 'CommentController@create');

Post Model

use App\User;
use App\Like;
use App\Comment;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;


class Post extends Authenticatable
{

    protected $fillable = [
        'title',
        'body',
        'user_id',
        'created_at',

    ];

   public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function likes()
    {
         return $this->hasMany('App\Like');
    }

    public function comments()
    {
        return $this->hasMany('App\Comment');
    }

Comment Model

class Comment extends Model
{
    protected $fillable = [
        'comment_body',
        'user_id',
        'post_id'

    ];

    public function user()
    {
        return $this->belongsTo('App\User'); 
    }

    public function post()
    {
        return $this->belongsTo('App\Post');
    }
}

CommentConroller

public function create(Request $request, Post $post)
{

    $data = request()->validate([
     'comment_body' => 'required|max:1000'
    ]);


    $data['user_id'] = auth()->user()->id;
    $data['name'] = auth()->user()->name;
    $data['post_id'] = $post->id;
    $post = Comment::create($data);



    $response = new Response(json_encode($data));
    $response->headers->set('Content-Type', 'application/json'); 

    if(!$response){
        return 'something went wrong';
    }

    return response()->json($data); 


}

Html

  <div class="comment-class animated bounceInUp" ng-show="writecomment">

                    <div class="panel-body">
                        <ng-form ng-model="commentForm" name="commentForm" method="POST" novalidate>
                        <div class="form-group">
                            <label>Write a Comment</label>


                            <textarea ng-model="post.comment" type="text" class="form-control" name="comment_body" cols="2" rows="2"></textarea>
                        </div>
                       <button id="eli-style-button" ng-click="addComment(post)" class="btn btn-primary" type="submit">Submit</button>
                        </form>
                    </div>
        <!-- END Comment form Inside Ng-repeat -->
                </div>
            <!-- End of ng-repeat post in mypost -->
            </div>

Main.js

$scope.addComment = function(post){

    $http.post('/post/comment',{
        comment_body: post.comment,
    }).then(function(result){
        console.log(result.data);
        $scope.myposts.push(result.data);
    });
};
1
In your CommentController, it looks like you’re only using $post to get the ID. Have you made sure that your Type-hinting is actually getting the Post? Because you do not have a parameter set for it in your routes file, nor does it appear you are passing Post into your http request in the Ajax function.Matt McAlister
type hinting what is that ? im going to try to do $post = Comment::create($data); $data['post_id'] = $post->id;BARNOWL
Check that your $post parameter is actually being set.Matt McAlister
i did this in tinker Comment::create(['comment_body' => 'this works', 'user_id'=> 1, 'post_id'=>8]); and it worksBARNOWL
Right, but here you are MANUALLY ensuring that 'post_id' has a value. There is no problem with the Model::create function. It appears evident that you are simply not passing in your $post parameter correctly. Remove Post $post, and pass the post id in with your comment_body in your Ajax call.Matt McAlister

1 Answers

2
votes

In order to use route model binding, you have to include the post as a parameter in your route:

Route::post('post/{post}/comment', 'CommentController@create');

Then call it like this:

 $http.post('/post/' + post.id + '/comment' ...

Right now in your controller you are getting an empty Post instance that has no ID.