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

Brammah's avatar

Excel Import, N+1 Issue

Hi, I need some help: I am using simple-excel package from spatie and I am able to import an excel document to the DB, the only caveat is that I am experiencing the N+1 problem when querying the import. Below is the store method of my controller:

  public function store(Request $request)
     {
        $this->validate($request, [
        'import_file'  => 'required|mimes:xls,xlsx,csv'
    ]);

    SimpleExcelReader::create($request->file('import_file'), 'xlsx')->getRows()
        ->each(function(array $rowProperties) {
        $user= User::where('name', 'like', '%'. $rowProperties['username'].'%')->first();

        Department::create([
                'user_id' => $user->id,
                'name' => $rowProperties['name'],
                'code' => $rowProperties['code']
        ]);
    });

    return back()->with('success', 'All good!');
}

Kindly help me rewrite the code $user= User::where('name', 'like', '%'. $rowProperties['username'].'%')->first(); in a more effecient way.

0 likes
28 replies
sr57's avatar

Where is your N+1 issue?

Brammah's avatar

@sr57 When I import, the 175 users, it executes 405 queries.

Snapey's avatar

You might be able to read all users into memory to avoid doing a search on every insert, but it depends how big the users table is

frankielee's avatar

$user= User::where('name', 'like', '%'. $rowProperties['username'].'%')->first();

The username is unique in the file?

Snapey's avatar
Snapey
Best Answer
Level 122

how often are you going to run this? - does it really matter?

1 like
Brammah's avatar

@Snapey Not frequently by the way. Thank you for this point of view.

sr57's avatar

@brammah

When I import, the 175 users, it executes 405 queries.

Where do you import the 175 users?

How many lines in your excel file?

sr57's avatar

@Brammah

So no N+1 pb, your code is correct regarding the number of queries.

1 like
Brammah's avatar

@sr57 Yeah, @snapey has made me notice that it doesn't really matter because I won't run the query frequently.

Sinnbeck's avatar

What is supposed to happen if this returns no results? Or a wrong match?

$user= User::where('name', 'like', '%'. $rowProperties['username'].'%')->first();

Let's say the file has the username "ra". Then it will just take the first user in the database that has "ra" anywhere in the username

Also, name and username is normally different things yet in your code they are the same?

Sinnbeck's avatar

@Brammah but no user in the system has a name/username that is a part of another's? Do you understand what I'm pointing out? :)

Brammah's avatar

@Sinnbeck if it returns no result, on the $user->id, i can rewrite it to $user->id ?? null;

Sinnbeck's avatar

@Brammah it will already be null. So if that's allowed, then that part is good

1 like
Brammah's avatar

@Sinnbeck Maybe I can rename the username in the excel file to read name and move the columns around.. :)

Sinnbeck's avatar

@Brammah it's completely up to you. I don't know your database etc :)

I am mostly just pointing out that you code might find a wrong user

Brammah's avatar

@Sinnbeck

  {
     Schema::create('users', function (Blueprint $table) {
      $table->id();
      $table->string('name');
      $table->string('username')->unique();
      $table->string('email')->unique();
      $table->string('phone');
      $table->string('code')->unique();
      $table->string('gender');
      $table->string('password');
      $table->string('status')->default('active');
      $table->rememberToken();
      $table->timestamps();
    });
  }```
Sinnbeck's avatar

@Brammah so you have both username and name. But you use the username column in the excel file to compare to the name column (not username) in the database?

Did you mean to do

$user= User::where('username', '=', $rowProperties['username'])->first();
Brammah's avatar

@Sinnbeck If I use the = the code will throw an error, Attempt to read property "id" on null.

But since the system doesn't frequently upload users, I will have to go with @snapey 's and @sr57 's directive, it doesn't really matter because the frequency of upload is not that much.

Sinnbeck's avatar

@Brammah yeah those are the answers for the question and are therefor best. I was only pointing out a potential problem with your code, which was unrelated to the question :)

Please or to participate in this conversation.