gpt4 book ai didi

mysql - 违反完整性约束 : 1048 Column 'post_id' cannot be null

转载 作者:行者123 更新时间:2023-11-29 05:57:56 24 4
gpt4 key购买 nike

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

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

我将使用:<% %>是为了angular,只是让大家知道。

在 tinker 中这行得通

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

评论 Controller

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

最佳答案

为了使用route model binding ,您必须将帖子作为参数包含在您的 route :

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

然后这样调用它:

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

现在在您的 Controller 中,您得到一个没有 ID 的空 Post 实例。

关于mysql - 违反完整性约束 : 1048 Column 'post_id' cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47620996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com