-1

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?

Shadow
  • 33,525
  • 10
  • 51
  • 64

1 Answers1

1

$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');
    }
}

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33
  • Why should the OP ___use this___? **Good answers** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. If it is well answered maybe they will UpVote it. – RiggsFolly Aug 25 '20 at 10:37
  • @RiggsFolly i was on the process of wiring my answer – Kamlesh Paul Aug 25 '20 at 10:38
  • 1
    @KamleshPaul I am getting error as, SQLSTATE[HY000]: General error: 1005 Can't create table `bizzcomputer`.`featured_products` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `featured_products` add constraint `featured_products_product_id_foreign` foreign key (`product_id`) references `products` (`id`) on delete cascade) –  Aug 25 '20 at 10:55
  • 1
    @Zee try now `bigInteger` data type was missing in Foreign key now it should work – Kamlesh Paul Aug 25 '20 at 10:58