为什么我会收到此错误? "Undefined variable: table"、"C:\....database\migrations20_08_25_082835_create_featured_products_table.php"、[])
Why I am getting this error? "Undefined variable: table", "C:\....database\migrations\2020_08_25_082835_create_featured_products_table.php", [])
我正在 laravel 使用 mysql 数据库进行开发。
这是我得到的错误。
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined variable: table", "C:\xampp\htdocs\bizzcomputer\database\migrations20_08_25_082835_create_featured_products_table.php", [])
这个产品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');
}
}
这个featured_productstable
<?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');
}
}
这有什么问题吗?
是否可以使用外键和复合键?
$table
未定义,因为您的外键定义是在 Schema::create()
上下文之外编码的。
所以像这样把它移回里面
<?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');
}
}
我正在 laravel 使用 mysql 数据库进行开发。
这是我得到的错误。
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined variable: table", "C:\xampp\htdocs\bizzcomputer\database\migrations20_08_25_082835_create_featured_products_table.php", [])
这个产品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');
}
}
这个featured_productstable
<?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');
}
}
这有什么问题吗? 是否可以使用外键和复合键?
$table
未定义,因为您的外键定义是在 Schema::create()
上下文之外编码的。
所以像这样把它移回里面
<?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');
}
}