Skip to content

A bug with the refresh method (Model class)? #27288

Closed
@djug

Description

@djug
  • Laravel Version: 5.7.13
  • PHP Version: 7.3.1
  • Database Driver & Version: mysql Ver 15.1 Distrib 10.3.12-MariaDB

Description:

in Illuminate/Database/Eloquent/Model.php
if we take a look at the fresh method we find this at the beginning:

if (! $this->exists) {
   return;
}

which mean if the model is no longer there we just return (null?).

but the behavior of the refresh method is slightly different:

if (! $this->exists) {
   return $this;
}

in other terms, if we delete the model, and we execute the fresh() method on it, we will get null, but if we execute refresh() we will get the model back again, which, if I understand correctly, is not the intended goal of this method.

Steps To Reproduce:

>>> $user = User::first()
[!] Aliasing 'User' to 'App\User' for this Tinker session.
=> App\User {#2919
     id: 3,
     name: "Sienna Becker",
     email: "cbrekke@example.net",
     email_verified_at: "2018-11-16 06:21:34",
     created_at: "2018-11-16 06:21:34",
     updated_at: "2018-11-16 06:21:34",
   }
>>> $user->delete()
=> true
>>> $user->fresh()
=> null
>>> $user->refresh()
=> App\User {#2919
     id: 3,
     name: "Sienna Becker",
     email: "cbrekke@example.net",
     email_verified_at: "2018-11-16 06:21:34",
     created_at: "2018-11-16 06:21:34",
     updated_at: "2018-11-16 06:21:34",
   }
>>> 

if someone could confirm that this is a bug I'll send a MR for it

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