【发布时间】:2021-04-09 12:14:50
【问题描述】:
有人知道这个问题吗? 我正在使用 Laravel 8,我真的不知道是哪个问题。 我已经坚持了好几个月了。
错误按摩
\SQLSTATE[42000]: 语法错误或访问冲突: 1072 表中不存在键列 'kategori_id' (SQL: 更改表 produks 添加约束 produks_kategori_id_foreign 外键 (kategori_id) 引用 @987654324 @(id))
分类表
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateKategorisTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('kategoris', function (Blueprint $table) {
$table->id();
$table->string('nama');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('kategoris');
}
}
产品表
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProduksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('produks', function (Blueprint $table) {
$table->id();
$table->char('kode',6)->unique();
$table->string('nama');
$table->foreign('kategori_id')->references('id')->on('kategoris');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('produks');
}
}
类别工厂
<?php
namespace Database\Factories;
use App\Models\Kategori;
use Illuminate\Database\Eloquent\Factories\Factory;
class KategoriFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Kategori::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$daftar_kategori = ["ABB","3M", "Autonics", "Supreme", "Omron"];
return [
'id' => $this->faker->numberBetween(1, \App\Models\Kategori::count()),
'nama' => $this->faker->unique()->randomElement($daftar_kategori),
];
}
}
产品工厂
<?php
namespace Database\Factories;
use App\Models\Produk;
use Illuminate\Database\Eloquent\Factories\Factory;
class ProdukFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Produk::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$daftar_produk=["Inverter", "Rellay", "Lampu", "Push Button"];
return [
'kode' => strtoupper($this->faker->unique()->bothify('??###')),
'nama' => $this->faker->randomElement($daftar_produk),
'kategori_id' => $this->faker->numberBetween(1,
\App\Models\Kategori::count()),
];
}
}
【问题讨论】:
标签: php laravel eloquent migration