Новичок в 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);
}
}
create.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>
<div class="row">
<div class="col-8 offset-2">
<div class="form-group row">
<label for="image" class="col-md-4 col-form-label">Image</label>
<input type="file" class="form-control-file" id="image" name="image">
@error('file')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
</div>
<div class="row pt-4">
<div class="col-8 offset-2">
<div class="form-group row">
<button class="btn btn-primary">Add post</button>
</div>
</div>
</div>
</form>
</div>
</body>
@endsection
PostsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function create(){
return view('posts.create');
}
public function store(){
$data = request()->validate([
'title',
'thought',
'image',
'url'
]);
auth()->user()->posts()->create($data);
dd(request()->all());
}
}
И, наконец, таблица:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->text('title');
$table->text('thought');
$table->string('url')->nullable();
$table->string('image')->nullable();
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Любой совет будет потрясающим. Если вам нужна какая-либо другая информация, пожалуйста, дайте мне знать.