Certainly! Here's a clear approach for having two separate importers for the same Contract model: one for creating and one for updating. You might be using a package like Laravel Excel for imports, but the architectural pattern stands with or without a package.
Approach Summary:
- CreateNewContractsImporter: Only creates new
Contractrecords, ignores existing ones. - UpdateExistingContractsImporter: Only updates existing
Contractrecords, ignores new ones.
Your first is already built (CreateNewContractsImporter). Here’s how you could structure the update-only importer:
Update Only Importer
use Maatwebsite\Excel\Concerns\ToModel;
use App\Models\Contract;
class UpdateExistingContractsImporter implements ToModel
{
public function model(array $row)
{
// Assume your unique identifier is in $row[0], adjust as necessary
$contract = Contract::where('contract_number', $row[0])->first();
// Only update if contract exists
if ($contract) {
$contract->update([
'field1' => $row[1],
'field2' => $row[2],
// ... other fields
]);
}
// If you always need to return a model or null:
return null;
}
}
Tips
- Make sure the "unique identifier" is the same as used in your system (
id,contract_number, etc.). - No new record is created here;
.update()only fires for existing. - Returning
null(or justvoid) is okay if your importer supports it―otherwise, return the updated model. - If you need validation, consider implementing
WithValidationor similar concerns.
Optional: Validation to Ensure No New Contracts
If you want to be extra safe:
if (!$contract) {
// Optionally, log or collect errors about "missing contracts"
return null; // Or throw an exception if you prefer
}
Summary
- Keep two separate importer classes, each with a very explicit, limited responsibility.
- Creation: Only insert new records (skip/update errors if record exists).
- Update: Only update existing records (skip/memo errors if not found).
This maximizes clarity and keeps your logic separation just as your client requested.
If you need a more specific example (like for a plain PHP upload script), let me know!