我用它们的迁移表创建了以下模态:
- 页面迁移表**
return new class extends Migration
{
public function up()
{
Schema::create('pages', function (Blueprint $table) {
$table->id();
$table->string('page_name_en');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('pages');
}
};
- 页面模态**
class Page extends Model
{
use HasFactory;
//this will make all fields mass assignable
protected $guarded = [];
}
- Carousel_Sections迁移表格**
return new class extends Migration
{
public function up()
{
Schema::create('carousel_sections', function (Blueprint $table) {
$table->id();
$table->bigInteger('page_id')->unsigned();
$table->index('page_id');
$table->foreign('page_id')->references('id')->on('pages')->onDelete('cascade')->onUpdate('cascade');
$table->integer('order');
$table->integer('slide_no');
$table->string('image')->nullable();
$table->string('title_en')->nullable();
$table->string('short_title_en')->nullable();
$table->string('button_text_en')->nullable();
$table->string('button_link')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('carousel_sections');
}
};
- 转盘部分模态**
class CarouselSection extends Model
{
use HasFactory;
//this will make all fields mass assignable
protected $guarded = [];
}
然后我迁移了表,没有出现任何问题。
然后运行以下命令:
php artisan db:seed
出现以下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'page_id ' in 'field list' (SQL: insert into `carousel_sections` (`page_id `, `order`, `slide_no`, `image`, `title_en`, `short_title_en`, `button_text_en`, `button_link`, `updated_at`, `created_at`) values (1, 1, 1, ?, Fisrt Title, first short title, learn more, www.google.com, 2022-12-19 09:07:34, 2022-12-19 09:07:34))
我该如何解决这个问题?
- 注意:**page_id是一个外键,它引用"页"表上的id。
下面是我的DatabaseSeeder代码:
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\Page::factory()->create([
'page_name_en' => 'Home'
]);
\App\Models\CarouselSection::factory()->create([
'page_id ' => 1,
'order' => 1,
'slide_no' => 1,
'image' => null,
'title_en' => 'Fisrt Title',
'short_title_en' => 'first short title',
'button_text_en' => 'learn more',
'button_link' => 'www.google.com'
]);
}
}
1条答案
按热度按时间bnlyeluc1#
查看错误消息
第一个月
您可以看到在列名
page_id
的末尾包含了一个空格。导致此错误的代码位于
DatabaseSeeder.php
文件中:需要将
'page_id ' => 1
更改为'page_id' => 1
。