Win*_*han 2 php migration laravel eloquent
有人对这个问题有任何想法吗?我正在使用 Laravel 8,我真的不知道哪个问题。我已经坚持了这个多月了。
错误按摩
\SQLSTATE[42000]:语法错误或访问冲突:1072 表中不存在键列“kategori_id”(SQL:alter tableproduks添加约束produks_kategori_id_foreign外键(kategori_id)引用kategoris(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');
}
}Run Code Online (Sandbox Code Playgroud)
产品表
<?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');
}
}Run Code Online (Sandbox Code Playgroud)
类别工厂
<?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),
];
}
}Run Code Online (Sandbox Code Playgroud)
产品工厂
<?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()),
];
}
}Run Code Online (Sandbox Code Playgroud)
小智 7
When you add a foreign key in the Schema::create, you need to add the respective field first, like this:
$table->unsignedBigInteger('foreing_id');
Run Code Online (Sandbox Code Playgroud)
And after that, transform it into a foreign key with the:
$table->foreign('foreign_id')->references('id')->on('foreign_table');
Run Code Online (Sandbox Code Playgroud)
You miss the foreign field in the table products:
Schema::create('produks', function (Blueprint $table) {
$table->id();
$table->char('kode',6)->unique();
$table->string('nama');
add this--> $table->unsignedBigInteger('kategori_id');
$table->foreign('kategori_id')->references('id')->on('kategoris');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
Run your migrations again and the problem should dissapear.