Search code examples
phplaravelforeign-keyscomposite-key

Why I am getting this error? "Undefined variable: table", "C:\....database\migrations\2020_08_25_082835_create_featured_products_table.php", [])


I am developing in laravel with mysql database.

This is the error which I got.

Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined variable: table", "C:\xampp\htdocs\bizzcomputer\database\migrations\2020_08_25_082835_create_featured_products_table.php", [])

This product table

<?php

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

class CreateProductsTable extends Migration
{
 
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('prod_name');
            $table->string('prod_brand')->nullable();
            $table->unsignedBigInteger('category_id');
            $table->timestamps();
    
            $table->foreign('category_id')
                ->references('id')
                ->on('categories')
                ->onDelete('cascade');
        });
    }

    public function down()
    {
        Schema::dropIfExists('products');
    }
}

This featured_products table

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFeaturedProductsTable extends Migration
{       
    public function up()
    {
        
        Schema::create("featured_products", function($table) {
            $table->increments('id');
            $table->integer('product_id');
            $table->timestamps();
        });

        $table->foreign('product_id')
            ->references('id')
            ->on('products')
            ->onDelete('cascade');

        DB::unprepared('ALTER TABLE `featured_products` DROP PRIMARY KEY, ADD PRIMARY KEY (  `id` ,  `product_id` )');

    }  
    public function down()
    {
        Schema::dropIfExists('featured_products');
    }
}

What is the wrong with this? Is it possible to use foreign key and composite key?


Solution

  • $table is undefined because your foreign key definition is coded outside of the Schema::create() context.

    So move it back inside like this

    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateFeaturedProductsTable extends Migration
    {
        public function up()
        {
    
            Schema::create("featured_products", function ($table) {
                $table->increments('id');
                $table->bigInteger('product_id');
                $table->timestamps();
                $table->foreign('product_id')
                    ->references('id')
                    ->on('products')
                    ->onDelete('cascade');
    
            });
    
                DB::unprepared('ALTER TABLE `featured_products` DROP PRIMARY KEY, ADD PRIMARY KEY (  `id` ,  `product_id` )');
    
        }
        public function down()
        {
            Schema::dropIfExists('featured_products');
        }
    }