Using Laravel 5.8.31 I seem to be getting an integrity constraint violation for something in a table that should not be null. The issue is that its telling me posts.title in my migration file should not be null, but that variable is not even in the migration table.
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint >failed: posts.title (SQL: insert into "posts" ("caption", "image", "user_id", >"updated_at", "created_at") values (kjhgj, C:\xampp\tmp\phpE3B7.tmp, 1, 2019->08-14 07:14:03, 2019-08-14 07:14:03))
I am trying to learn Laravel and have been following this youtube tutorial https://www.youtube.com/watch?v=ImtZ5yENzgE
From the error page that I get from my browser I can see that the error is thrown in PostsController.php.
I made it to time 2:04:00 but ran into this issue. I have read many other questions asked and answered here, but with no luck.
Posts Migration File
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->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
// THERE IS NO 'title' entry.
});
}
Posts Model File
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//Tutorial uses guarded rather than fillable
protected $guarded = [];
public function user(){
return $this->belongsTo(User::class);
}
}
Posts Controller File
class PostsController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store()
{
$data = request()->validate([
'caption' => 'required',
'image' => ['required','image'],
]);
auth()->user()->posts()->create($data); //ERROR IS THROWN HERE
dd(request()->all());
}
}
I have a page where the user enters a caption and uploads file.
Without any validation I can dd(request()->all());
to see that the caption and the file were received. But when I add the validation and the auth()
line in the controller file I get the integrity constraint violation for "posts.title"
I expect the caption and the image to be added to the database.
First time posting because I can usually find the answer, but this time I'm stuck.
Edit: here is the error: Error Image
Solved
@Vipertecpro was able to solve my issue. I had to run the following two commands:
php artisan migrate:refresh --seed
then php artisan optimize:clear
.
@Don'tPanic Explained why the commands worked in the comments of this post.