【发布时间】:2021-06-10 13:29:22
【问题描述】:
我正在尝试创建一个创建页面,其中用户在表单中输入标题并选择图像,但是当我单击按钮添加新帖子时,我收到此错误:
SQLSTATE[23000]:违反完整性约束:19 NOT NULL 约束失败:posts.user_id(SQL:插入“posts”(“caption”、“image”、“updated_at”、“created_at”)值(asd, C:\xampp\tmp\phpB7DC.tmp, 2021-03-12 13:06:56, 2021-03-12 13:06:56))
我想告诉你我正在使用一个 sqlite 数据库。
邮政模式:
class Post extends Model
{
protected $fillable = [
'caption', 'image',
];
public function user()
{
return $this->belognsTo(User::class);
}
}
PostsController:
class PostsController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store() {
$data = request()->validate([
'caption' => 'required',
'image' => 'required|image',
]);
\App\Post::create($data);
dd(request()->all());
}
}
迁移文件夹中我的表格帖子:
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');
});
}
【问题讨论】:
-
您没有在帖子中添加 user_id。
标签: database laravel sqlite model-view-controller