Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ene's avatar
Level 2

creating a notification table file

please who can help me create this as migration file

---- Database: `---- ------------------------------------------------------------ Table structure for table `notifications`--CREATE TABLE `notifications` (  `id` bigint(20) NOT NULL,  `sender_id` bigint(20) UNSIGNED NOT NULL,  `recipient_id` bigint(20) UNSIGNED NOT NULL,  `item_id` bigint(20) UNSIGNED DEFAULT NULL,  `notification_type` text COLLATE utf8mb4_unicode_ci NOT NULL,  `like_id` bigint(20) UNSIGNED DEFAULT NULL,  `comment_id` int(11) DEFAULT NULL,  `seen` enum('1','2') COLLATE utf8mb4_unicode_ci NOT NULL,  `created_at` timestamp NULL DEFAULT NULL,  `updated_at` timestamp NULL DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
0 likes
6 replies
delyn12's avatar

You want to have it in Laravel migration file?

delyn12's avatar
delyn12
Best Answer
Level 1

should be like this if am not mistaken

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNotificationTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('notifications', function (Blueprint $table) {
            $table->id();
            $table->bigint('sender_id');
            $table->bigint('recipient_id');
            $table->bigint('item_id')->nullable();
            $table->string('notification_type');
            $table->bigint('like_id')->nullable();
            $table->int('comment_id')->nullable();
            $table->int('difficulty', ['1', '2']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('notifications');
    }
}
ene's avatar
Level 2

@delyn12 this exactly what I needed thanks let's me give it a try

ene's avatar
Level 2

@delyn12 i got this error PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key")

 Schema::create('notifications', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('sender_id');
            $table->biginteger('recipient_id');
            $table->biginteger('item_id')->nullable();
            $table->string('notification_type');
            $table->biginteger('like_id')->nullable();
            $table->integer('comment_id')->nullable();
            $table->integer('seen', ['1', '2']);
            $table->timestamps();
        });
delyn12's avatar

@ene Try

 Schema::create('notifications', function (Blueprint $table) {
            $table->id();
            $table->foreignId('sender_id');
            $table->foreignId('recipient_id');
            $table->foreignId('item_id')->nullable();
            $table->string('notification_type');
            $table->foreignId('like_id')->nullable();
            $table->foreignId('comment_id')->nullable();
            $table->integer('seen', ['1', '2']);
            $table->timestamps();
        });   

Please or to participate in this conversation.