【问题标题】:Why I am getting this error? "Undefined variable: table", "C:\....database\migrations\2020_08_25_082835_create_featured_products_table.php", [])为什么我收到此错误? “未定义变量:表”,“C:\....数据库\迁移\2020_08_25_082835_create_featured_products_table.php”,[])
【发布时间】:2020-12-14 00:52:29
【问题描述】:

我正在使用 mysql 数据库在 laravel 中开发。

这是我得到的错误。

Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("未定义变量:表", "C:\xampp\htdocs\bizzcomputer\database\migrations\2020_08_25_082835_create_featured_products_table.php", [])

此产品表

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

此特色产品表

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

这有什么问题? 是否可以使用外键和复合键?

【问题讨论】:

  • 欢迎来到 SO ... 变量不会凭空创造出来

标签: php laravel foreign-keys composite-key


【解决方案1】:

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

【讨论】:

  • OP 为什么要使用这个好的答案将始终解释所做的工作以及这样做的原因,不仅适用于 OP,而且适用于 SO 的未来访问者,他们可能会发现这个问题并正在阅读您的答案。如果回答得很好,也许他们会支持它。
  • @RiggsFolly 我正在整理我的答案
  • @KamleshPaul 我收到错误,SQLSTATE[HY000]:一般错误:1005 无法创建表 bizzcomputer.featured_products(错误号:150“外键约束格式不正确”) (SQL:更改表featured_products添加约束featured_products_product_id_foreign外键(product_id)引用productsid)删除级联)
  • @Zee 立即尝试 bigInteger 外键中缺少数据类型,现在它应该可以工作了
猜你喜欢
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
相关资源
最近更新 更多