Skip to content
Advertisement

Integrity constraint violation in laravel

enter image description hereenter image description hereI am passing the ID in URL to get to the Form Page I want but I want to pass in the same ID when I hit the submit button and I want to pass in the same ID to the database table name “created_by”

Here’s My Route:-

Route::get('/post/{id}', 'PostsController@create');

Here’s My Controller:-

public function create(){
  return view('Posts.CreatePost');

}

public function store(Request $request){
    post::create([
              'title'=>$request->title,
              'body'=>$request->body,
              'user_id'=>Auth::user()->id,
              'filled_by'=>Auth::user()->id,
              'created_by' => $request->route('id'),
            ]);

    return redirect('/profile/' . auth()->user()->id);
    }

Here’s My Migration:-

public function up(){
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->text('body');
            $table->string('created_by');
            $table->string('filled_by');
            $table->timestamps();

            $table->index('user_id');
        });
    }

Here’s My Model:-

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Post extends Model
{ protected $gaurded = [];

  protected $fillable = ['title', 'body', 'user_id', 'created_by', 'filled_by'];

    public function user()
    {
      return $this->belongsTo(User::class);
    }
}

The Error that i am getting is SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘created_by’ cannot be null (SQL: insert into posts (title, body, user_id, filled_by, created_by, updated_at, created_at) values (adsa, asdas, 1, 1, ?, 2020-04-16 20:54:30, 2020-04-16 20:54:30))

I am just a beginner at programming and laravel so excuse me if I am being stupid

-ThankYou

Advertisement

Answer

You are getting this error because $request->route('id') is empty and your database doesn’t allow for the created_by column to be empty.

To debug, check the content of your request:

public function store(Request $request){

    // Die and dump the request:
    dd($request->all());

    post::create([
              'title'=>$request->title,
              'body'=>$request->body,
              'user_id'=>Auth::user()->id,
              'filled_by'=>Auth::user()->id,
              'created_by' => $request->route('id'),
            ]);

    return redirect('/profile/' . auth()->user()->id);
    }

I believe you are not correctly retrieving the value id from the request.

You probably want to retrieve it with $request->id but it’s hard to say without looking at your blade view. If you can share your view I’ll edit my answer.


Edit: Also, your route is incorrect. To point to your controller create() method, you shouldn’t have to pass an id to your route. By definition, you want to CREATE an object that doesn’t yet exist and therefore doesn’t yet have an ID.

With the create() method, your route should look like this:

Route::get('/post/create', 'PostsController@create');

Also, if you want to follow Laravel naming conventions (trust me, you do), your controllers should be named in singular and url models in plural. Meaning it would look like this:

Route::get('/posts/create', 'PostController@create');

And the route for your store() method should look like this:

Route::post('/posts', 'PostController@store');
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement