Skip to content

Update Laravel Excel docs with upsert columns explanation #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 3.1/imports/concerns.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pageClass: no-toc
|`Maatwebsite\Excel\Concerns\WithCustomCsvSettings`| Allows to run custom Csv settings for this specific importable. | [Custom CSV Settings](/3.1/imports/custom-csv-settings.html) |
|`Maatwebsite\Excel\Concerns\WithStartRow`| Define a custom start row. | |
|`Maatwebsite\Excel\Concerns\WithProgressBar`| Shows a progress bar when uploading via the console. | [Progress Bar](/3.1/imports/progress-bar.html) |
|`Maatwebsite\Excel\Concerns\WithUpsert`| Allows to upsert models. | [Upserting models](/3.1/imports/model.html#upserting-models) |
|`Maatwebsite\Excel\Concerns\WithUpsertColumns`| Allows upsert columns definition. | [Upserting with specific columns](/3.1/imports/model.html#upserting-with-specific-columns) |
|`Maatwebsite\Excel\Concerns\WithValidation`| Validates each row against a set of rules. | [Row Validation](/3.1/imports/validation.html) |
|`Maatwebsite\Excel\Concerns\SkipsOnFailure`| Skips on validation errors. | [Row Validation](/3.1/imports/validation.html#skipping-failures) |
|`Maatwebsite\Excel\Concerns\SkipsOnError`| Skips on database exceptions. | [Row Validation](/3.1/imports/validation.html#skipping-errors) |
Expand Down
19 changes: 19 additions & 0 deletions 3.1/imports/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ In the example above, if a user already exists with the same email, the row will
All databases except SQL Server require the `uniqueBy` columns to have a "primary" or "unique" index.
:::

### Upserting with specific columns

By default, `upsert`, in case of updating, will update all columns that match model's attributes. However, if you need to update only specific column(s) during `upsert`, you can also implement the `WithUpsertColumns` concern.

```php
class UsersImport implements ToModel, WithUpserts, WithUpsertColumns
{
/**
* @return array
*/
public function upsertColumns()
{
return ['name', 'role'];
}
}
```

In this example, if a user already exists, only "name" and "role" columns will be updated.

## Skipping rows

In case you want to skip a row, you can return null.
Expand Down