Reputation: 338
New to Laravel, trying to give the user the ability to create a post. When I click submit I get this error:
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))
I have seen similar questions to this but none got a good answer. Here is my model:
<?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());
}
}
And lastly the table:
<?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');
}
}
Any advice would be awesome. If you need any other information please let me know.
Upvotes: 0
Views: 369
Reputation: 4813
You are missing name="title" for title input
//...
<input id="title" name="title" type="text" class="form-control @error('title') is-invalid @enderror" title="title" value="{{ old('title') }}" required autocomplete="title" autofocus>
Update
You are missing also validation rules
Check docs
//...
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'thought' => 'required',
'image' => 'required|image',
'url' => 'required|url'
]);
auth()->user()->posts()->create($validatedData);
Upvotes: 1