违反完整性约束:1048列"post

编程入门 行业动态 更新时间:2024-10-28 19:21:03
本文介绍了违反完整性约束:1048列"post_id"不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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]:违反完整性约束:1048列 'post_id'不能为null(SQL:插入comments(comment_body, user_id,post_id,updated_at,created_at)值(sdsd,1 、、 2017-12-03 12:29:58,2017-12-03 12:29:58))

"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:<% %>是针对角度的,只是让大家知道.

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

在修补程序中有效

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="postment" 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: postment, }).then(function(result){ console.log(result.data); $scope.myposts.push(result.data); }); };

推荐答案

为了使用路线模型绑定,您必须在路线中将帖子作为参数添加进来:

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

然后这样称呼它:

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

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

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

更多推荐

违反完整性约束:1048列"post

本文发布于:2023-10-22 20:58:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:完整性   post   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!