Laravel-excel: Importing data by multiple possible row names

Created on 26 Mar 2019  路  5Comments  路  Source: Maatwebsite/Laravel-Excel

Versions

  • PHP version: 7.3
  • Laravel version: 5.7.28
  • Package version: 3.1

Description

Is possible to import data by various row names? An example below will explain my question further. Thanks!

Additional Information

I want to import a name of the new Post model from the column with name "_Client name_" OR "_Client_" OR simply from the first columns.

public function model(array $row) {
    return new Post([
        'name' => $row['client_name'] || $row['client'] || $row[0]
    ]);
}
question

Most helpful comment

I got it finally in really simple way!! In my opinion, this could be documented because for someone it can save hours of trying and searching... :)

It works like optional column names with the priority descending from left to right and finally if there's no any match, return null for specific array value.

In words: If $row contains client_name, take its value. If not, test client and if this isn't set as well, just return null.

public function model(array $row) {
  return new Post([
    'name' => $row['client_name'] ?? $row['client'] ?? null
  ]);
}

All 5 comments

Not sure if that's possible with our package, but that would mean an import file with multiple columns.
How should be determined what value to take / e.g. what if there are multiple columns that have a value?

Thanks for your answer. It should work based on priority, the first optional row name (= $row['client_name']) have the highest priority and if exists, take this value). Whether the $row['client_name'] doesn't exist, try $row['client'] and if still not, simply take the first one $row[0].

The main idea is to give end-user easier formating of table which will be imported. Thanks to this, he don't have to format table only in one way. Thanks.

I got it finally in really simple way!! In my opinion, this could be documented because for someone it can save hours of trying and searching... :)

It works like optional column names with the priority descending from left to right and finally if there's no any match, return null for specific array value.

In words: If $row contains client_name, take its value. If not, test client and if this isn't set as well, just return null.

public function model(array $row) {
  return new Post([
    'name' => $row['client_name'] ?? $row['client'] ?? null
  ]);
}

Glad to hear you got it working!
Pull Requests to the docs are always welcome. You can use the "Help us improve this page!" at the bottom of the documentation pages.
If we think the information would be helpful for most users, we're happy to have a look at it and merge it.

I got it finally in really simple way!! In my opinion, this could be documented because for someone it can save hours of trying and searching... :)

It works like optional column names with the priority descending from left to right and finally if there's no any match, return null for specific array value.

In words: If $row contains client_name, take its value. If not, test client and if this isn't set as well, just return null.

public function model(array $row) {
  return new Post([
    'name' => $row['client_name'] ?? $row['client'] ?? null
  ]);
}

it worked for me

Was this page helpful?
0 / 5 - 0 ratings