Laravel: Integrity constraint violation

2551 views php
1

I'm studying some Laravel and at some point I had to re-migrate the database because I had to change a table. I'm using postman to do testing, and one of the api methods give me the error:

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: events.user_id (SQL: insert into "events" ("sport", "title", "players", "when", "description", "location", "updated_at", "created_at") values (Hockey, Grass Hockey, 12, 30/09/2018, Come join us, Fairview park, 2018-11-08 22:19:45, 2018-11-08 22:19:45))

so it seems to be a problem with the events.user_id which I changed on a table called Events to have a relationship with the Users table. Some examples I found by researching is on table fields that were not ids, so I don't how to figure this one out, maybe some of you can help me!

here are the migrations for Events and Users:

 public function up()
    {
        Schema::create('events', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade');
            $table->string('sport');
            $table->string('title');
            $table->decimal('players', 8, 2);
            $table->date('when');
            $table->mediumText('description');
            $table->string('location');
            $table->timestamps();
        });
    }


public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

Here are the models:

class Event extends Model
{
    protected $fillable = [
        'sport',
        'title',
        'players',
        'when',
        'description',
        'location'
    ];

    public function user()
    {
        return $this->belongsTo('App\User');
    }

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function events()
    {
        return $this->hasMany('App\Event');
    }
}

And below is the api method that is giving me the error:

Route::post('/admin/create-event', function (Request $request) {
    $data = $request->all();

    $event = Event::create(
        [
            'sport' => $data['sport'],
            'title' => $data['title'],
            'players' => $data['players'],
            'when' => $data['when'],
            'description' => $data['description'],
            'location' => $data['location'],
        ]
    );

    return $event;
});

Thanks guys!

answered question

1 Answer

4

You need to pass the user_id:

'user_id' => \Auth::user()->id

The example above requires an authenticated user in the session, but if you are making the request using postman you probably don’t have one.

Anyway, you need to provide the user_id that will be stored in the database.

posted this

Have an answer?

JD

Please login first before posting an answer.