Laravel 的新手,试图让用户能够创建帖子。当我单击提交时,我收到此错误:
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: posts.title (SQL: insert into "posts" ("user_id", "updated_at", "created_at") values (1, 2020-01-23 04:19:50, 2020-01-23 04:19:50))
我见过与此类似的问题,但没有一个得到很好的答案。这是我的模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $gaurded = [];
protected $fillable = ['title', 'thought', 'image', 'url'];
public function user(){
return $this->belongsTo(User::class);
}
}
创建.blade.php:
@extends('layouts.app')
@section('content')
<body>
<div class="container">
<form action="/p" enctype="multipart/form-data" method="post">
@csrf
<div class="row">
<div class="col-8 offset-2">
<h1>New Post:</h1>
<div class="form-group row">
<label for="title" class="col-md-4 col-form-label">Title</label>
<input id="title" name="title" type="text" class="form-control @error('title') is-invalid @enderror" title="title" value="{{ old('title') }}" required autocomplete="title" autofocus>
</div>
</div>
</div>
<div class="row">
<div class="col-8 offset-2">
<div class="form-group row">
<label for="thoughts" class="col-md-4 col-form-label">Thoughts</label>
<textarea type="text" class="form-control @error('thoughts') is-invalid @enderror" id="thoughts" name="thoughts"></textarea>
@error('file')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
</div>
拉风的咖菲猫
幕布斯6054654