Home > Software engineering >  Laravel - Column not found: 1054 Unknown column error after running php artisan db:seed
Laravel - Column not found: 1054 Unknown column error after running php artisan db:seed

Time:12-19

I created the following modals with thier 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 Modal

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 Modal

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

}
}

CodePudding user response:

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.

  • Related