Skip to content

Eloquent collection gets null in-place of a deleted model on $collection->fresh()  #32577

@aleedhillon

Description

@aleedhillon
  • Laravel Version: 7.8.1
  • PHP Version: 7.4.4
  • Database Driver & Version: MySQL v5.7.29

Description:

When we delete an model from an Eloquent collection like so

$collection->find($id)->delete()

or just

Model::find($id)->delete()

and then to get the latest data use

$collection = $collection->fresh()

it return null in the place of deleted model. And then if you send this collection to your view you will obviously get error trying to get property of null.
So shouldn't fresh() method just remove that deleted model from the collection instead of placing a null in it's place or this is the intended behavior and we should instead do

$collection = Model::all()

to get the latest data after a delete.

Steps To Reproduce:

Fetch an Eloquent collection of any model from database and then delete any model that is in that collection and then use the $collection->fresh() method to get the fresh data from database.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions