添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I created the following models with their migration tables:

Pages migration table

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

Page Model

class Page extends Model
use HasFactory;
//this will make all fields mass assignable
protected $guarded = [];

Carousel_Sections migration table

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

CarouselSection Model

class CarouselSection extends Model
use HasFactory;
//this will make all fields mass assignable
protected $guarded = [];

Then I migrated the tables with no problems.

And then after running this command:

php artisan db:seed

I get the following error:

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)

How can I solve this??

Note: page_id is a foreign key that reference the id on Pages table.

Below is my DatabaseSeeder code :

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'
                You have a space at the end of page_id when creating the carousel section in the seeder file, 'page_id ' => 1 should be 'page_id' => 1.
– RJK
                Dec 19, 2022 at 10:08
                No worries, we all do it, just so you know the way I noticed the space was from the error message Column not found: 1054 Unknown column 'page_id ' in 'field list', the column name instantly jumped out at me, from there I just looked for the code which contained this column name (including the space).
– RJK
                Dec 19, 2022 at 10:22

Looking at the error message

Column not found: 1054 Unknown column 'page_id ' in 'field list'

You can see that a space is being included at the end of the column name page_id.

The code that causes this error is in the DatabaseSeeder.php file:

\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'

'page_id ' => 1 needs to be changed to 'page_id' => 1.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.