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))我见过与此类似的问题,但没有一个得到很好的答案。这是我的模型:<?phpnamespace 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>
2 回答
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
您缺少用于标题输入的 name="title"
//... <input id="title" name="title" type="text" class="form-control @error('title') is-invalid @enderror" title="title" value="{{ old('title') }}" required autocomplete="title" autofocus>
更新
您还缺少验证规则
检查文档
//...
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'thought' => 'required',
'image' => 'required|image',
'url' => 'required|url'
]);
auth()->user()->posts()->create($validatedData);
- 2 回答
- 0 关注
- 262 浏览
添加回答
举报
0/150
提交
取消