Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mehmetanbaki's avatar

big error import

Hey Laravelers:

this is my code and I don't know how to fix it:

I try to create a table in the database when I import it I'm using laravel-excel

this is import-model:


<?php

namespace App\Imports;

use App\People;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;


class PeopleIMport implements OnEachRow
{

        public function onRow(Row $row)
        {
            People::firstOrCreate($row->toArray());
        }
        public function createTable($table_name, $fields = [])
        {
            if (!Schema::hasTable($table_name)) {
                Schema::create($table_name, function (Blueprint $table) use ($fields, $table_name) {
                    $table->increments('id');
                    if (count($fields) > 0) {
                        foreach ($fields as $field) {
                            $table->{$field['type']}($field['name']);
                        }
                    }
                    $table->timestamps();
                });
            }
        }

}


this is the controller:


<?php

namespace App\Http\Controllers;

use App\People;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use App\Exports\PeopleExport;
use App\Imports\PeopleImport;
use Maatwebsite\Excel\Facades\Excel;

class PeopleController extends Controller
{

    public function index()
    {
        $people = People::all();

        return view('people')->with('people', $people);
    }


    public function import(Request $request)
    {
        if ($request->file('imported_file')) {
            Excel::import(new PeopleImport(), request()->file('imported_file'));
            return back();
        }
    }

    public function export()
    {
        return Excel::download(new PeopleExport(), 'people.xlsx');
    }

}


this is the error:


Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `people` where (`0` = semester and `1` = Industry and `2` = Data Base Name and `3` = Data Source and `4` = Data Base Description and `5` = Database Url and `6` = Data Dictionary Yes or No or Something? and `7` = Number of Rows and `8` = Number of Continous Variables and `9` = Number of Categorical Variables and `10` = Report (1) or Raw Data? (10) 1-10 scale and `11` = Comments) limit 1)
http://localhost:8000/people/import
Hide solutions
A column was not found

You might have forgotten to run your migrations. You can run your migrations using php artisan migrate.

Pressing the button below will try to run your migrations.
Read more

Run migrations

    Database: Running Migrations docs

what I'm trying to do importing randomly excel sheet

0 likes
6 replies
ahmeddabak's avatar

$row->toArray() returns the rows as indexed array like this

[ 0=> 'ABC', 2=> 'CDE' ]

which when you try to user it with People::firstOrCreate throws an error, because not field in the people's table is called 0.

you need to tell the excel importer that your excel file has headers by implementing the WithHeadingRow interface, then the importer will return an associative array

[ 'foo'=> 'ABC', 'bar'=> 'CDE' ]
mehmetanbaki's avatar

@ahmeddabak Ok that if I know what I'm importing but I'm importing random file and I don't know what's in it, So how could I do that ??

ahmeddabak's avatar

It is to complicated to import a file that you do not know its contents, even for a professional developer.

You file should have a predefined structure, or you will have to write a new importer for each file every time

mehmetanbaki's avatar

@ahmeddabak

I'm trying to do this importing file first compared it with the data the contains in my model and after that adding the new data to my table if it's not exist that first step

second step creating table if it's not exist as you can see here

        public function createTable($table_name, $fields = [])
        {
            if (!Schema::hasTable($table_name)) {
                Schema::create($table_name, function (Blueprint $table) use ($fields, $table_name) {
                    $table->increments('id');
                    if (count($fields) > 0) {
                        foreach ($fields as $field) {
                            $table->{$field['type']}($field['name']);
                        }
                    }
                    $table->timestamps();
                });
            }
        }

this to create a table if it's not exist

ahmeddabak's avatar

I recommend that you find a freelancer that implements this function for you.

Please or to participate in this conversation.