For some reason a user cannot delete a post if it has been liked, it was working before but when I linked posts with likes I have been getting this error, I can\'t even dele
Yes, it's your schema. The constraint on likes.post_id
will prevent you from deleting records from the posts
table.
One solution could be using onDelete('cascade')
in the likes
migration file:
Schema::create('likes', function (Blueprint $table) {
// Some other fields...
$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});
This way, when a post is deleted, all related likes will be deleted too.
Or, if you have a relationship from the Post model to the Like model, you can $post->likes()->delete()
before deleting the post itself.