完整性约束违规:1048列'post_id'不能为空

时间:2017-12-03 17:14:49

标签: mysql laravel

我正在使用laravel 5.5并且即时尝试在帖子中添加评论,当我提交表单时出现以下错误

  

“SQLSTATE [23000]:完整性约束违规:1048列   'post_id'不能为空(SQL:插入commentscomment_body,   user_idpost_idupdated_atcreated_at)值(sdsd,1 ,,   2017-12-03 12:29:58,2017-12-03 12:29:58))

即将使用:<% %>用于角度,让每个人都知道。

修补这个作品

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

**路线*

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

发布模型

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');
    }

评论模型

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 个答案:

答案 0 :(得分:2)

要使用route model binding,您必须在帖子中添加帖子作为参数:

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

然后这样称呼:

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

现在,在您的控制器中,您将获得一个没有ID的空Post实例。