Skip to content

[Bug]: PersistRelations only works on BelongsTo(Many) relations which are nullable #4277

@kerwin-wildsea

Description

@kerwin-wildsea

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

  • Yes, it's still reproducable

What version of Laravel Excel are you using?

3.1.63

What version of Laravel are you using?

11.43.2

What version of PHP are you using?

8.4

Describe your issue

Due to the ->save() call in src/Imports/Persistence/CascadePersistManager.php:44 which happens before the relations are persisted it is not possible to use the PersistRelations interface for relations which are required (NOT NULL).

How can the issue be reproduced?

  1. Create two models with a BelongsTo relation between them
  2. Make the relation ->foreignId(RelatedModel::class)->constrained() (NOT NULL)
  3. Create a ToModel import including $model->setRelation('related', new RelatedModel);
  4. Call import on the importer

What should be the expected behaviour?

I'd expect to either note in the docs that this is a limitation of the interface (a massive one) or that it'd not save the model before persisting the relations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions