Php SQLSTATE[23000]:完整性约束冲突:1048列';邮政编码';不能为空
我需要一些帮助…我正在用教程为标签创建一个函数,但是在我的数据库(Php SQLSTATE[23000]:完整性约束冲突:1048列';邮政编码';不能为空,php,database,laravel,Php,Database,Laravel,我需要一些帮助…我正在用教程为标签创建一个函数,但是在我的数据库(post\u-tag)中,我的post\u-id没有保存,只有tag\u-id和id。在我的posts表中,创建一篇帖子后,我收到了post的id,但是在这里,在我的post_标签中不是。你知道为什么男人 我的控制器 public function create(){ $tags = Tag::all(); return view('posts.create')->withTags($ta
post\u-tag
)中,我的post\u-id
没有保存,只有tag\u-id
和id
。在我的posts
表中,创建一篇帖子后,我收到了post的id
,但是在这里,在我的post_标签中
不是。你知道为什么男人
我的控制器
public function create(){
$tags = Tag::all();
return view('posts.create')->withTags($tags);
}
public function store(Request $request )
{
$data = request()->validate([
'caption' => 'required|max:255',
'image' => 'required|image',
]);
$post = new Post;
$post->tags()->sync($request->tags, false);
$imagePath = request('image')->store('uploads', 'public');
$image = Image::make(public_path("storage/{$imagePath}"))->fit(1600, 1100);
$image->save();
auth()->user()->posts()->create([
'caption' => $data['caption'],
'image' => $imagePath,
]);
return redirect('/profile/' . auth()->user()->id);
}
我的创建贴子标签表
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->references('id')->on('tags');
});
}
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
我的创建帖子表
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->references('id')->on('tags');
});
}
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
我可能错了,但由于您的表中没有与外键关联的onUpdate或onDelete属性,这可能是问题所在。您已经创建了一个新的Post对象,但尚未将其保存在此行中:
$post = new Post;
因此,在这一行中,紧接着:
$post->tags()->sync($request->tags, false);
此帖子的数据库中还没有id
(在帖子模型上也不会有id
),因此每次同步都会失败,因为它找不到id
新建Post对象后,save()
将其保存,然后可以进行同步
另一个可能在其他方面有所帮助的注意事项是,您的post模型的
id
有一个大int:
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
}
但是您的post_标记表只是一个整数。这种不匹配可能会导致一些问题。建议更改以匹配:
Schema::create('post_tag', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('post_id')->unsigned(); // <--- suggest change this to bigInteger
$table->foreign('post_id')->references('id')->on('posts');
Schema::create('post_标记',函数(Blueprint$表){
$table->bigIncrements('id');
$table->integer('post_id')->unsigned();//foreign('post_id')->references('id')->on('posts');
正确的解决方案是:
$post = auth()->user()->posts()->create([
'caption' => $data['caption'],
'image' => $imagePath,
]);
我刚刚将此添加到代码中,一切正常。我很确定这些表的模型中存在问题。由于您没有使用Laravel的命名表约定,您必须指定外键。也许我应该共享Post和Tag模型吗?我昨天回答了一些听起来非常类似的问题。请看这里: