title: What is the correct foreign key constraint syntax for a database migration in Laravel 8.X Eloquent?
body: <p>I am a Laravel and Eloquent noob.</p>
  <p>I'm trying to do a simple foreign key in a &quot;checklist items&quot; table that takes the primary ID from the &quot;users&quot; table as a foreign key.</p>
  <p>Here are my two database migrations:</p>
  <p>users:</p>
  <pre><code>&lt;?php
  
  use Illuminate\Database\Migrations\Migration;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Support\Facades\Schema;
  
  class CreateUsersTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('users', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;string('name');
              $table-&gt;string('email')-&gt;unique();
              $table-&gt;timestamp('email_verified_at')-&gt;nullable();
              $table-&gt;string('password');
              $table-&gt;rememberToken();
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('users');
      }
  }
  
  </code></pre>
  <p>items:</p>
  <pre><code>&lt;?php
  
  use Illuminate\Database\Migrations\Migration;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Support\Facades\Schema;
  
  class CreateItemsTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('items', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;integer('user_id')-&gt;unsigned();
              $table-&gt;string('itemName');
              $table-&gt;string('itemDescription');
              $table-&gt;timestamps();
          });
          
          Schema::table('items', function($table) {
              $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
          });
          
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('items');
      }
  }
  </code></pre>
  <p>attempting to run <code>php artisan migrate</code> yields the following error:</p>
  <pre><code>Migrating: 2014_10_12_000000_create_users_table
  Migrated:  2014_10_12_000000_create_users_table (0.49 seconds)
  Migrating: 2014_10_12_100000_create_password_resets_table
  Migrated:  2014_10_12_100000_create_password_resets_table (0.59 seconds)
  Migrating: 2019_08_19_000000_create_failed_jobs_table
  Migrated:  2019_08_19_000000_create_failed_jobs_table (0.33 seconds)
  Migrating: 2021_05_04_085648_create_items_table
  
     Illuminate\Database\QueryException
  
    SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `items` add constraint `items_user_id_foreign` foreign key (`user_id`) references `users` (`id`))
  
    at D:\Applications\laragon\www\ToDoApp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
      667|         // If an exception occurs when attempting to run a query, we'll format the error
      668|         // message to include the bindings with SQL, which will make this exception a
      669|         // lot more helpful to the developer instead of just the database's errors.
      670|         catch (Exception $e) {
    &gt; 671|             throw new QueryException(
      672|                 $query, $this-&gt;prepareBindings($bindings), $e
      673|             );
      674|         }
      675|
  
    1   D:\Applications\laragon\www\ToDoApp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
        PDOException::(&quot;SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint&quot;)
  
    2   D:\Applications\laragon\www\ToDoApp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
        PDOStatement::execute()
  </code></pre>
  <p>It should be noted that I can achieve what I need fine in the PHPMyAdmin GUI directly, or using basic PHP PDOs, but I don't understand Eloquent and need to understand what I am doing wrong.</p>
  <p>I have a feeling that it's to do with a mismatch in the attribute definitions between the two table migrations, but the few things I tried caused alternate errors.</p>