Lifted directly from @SteveTheBauman's tweet because I find myself needing this functionality very frequently https://twitter.com/SteveTheBauman/status/1524865758043553816
Example usage:
User.php
<?php
namespace App\Models;
use App\Concerns\TracksPreviousAttributes;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use TracksPreviousAttributes;
}
<?php
$user = User::create(['email' => '[email protected]']);
$user->update(['email' => '[email protected]']);
# ['email' => '[email protected]', ...]
$user->getPrevious();
# '[email protected]'
$user->getPrevious('email');