rain-plus/eloquent-inject-attribute

A helper class that injects attributes to Eloquent model.

1.0.1 2024-11-09 14:15 UTC

This package is auto-updated.

Last update: 2025-06-09 15:26:31 UTC


README

A helper class that can be used to inject attributes into an Eloquent model without having to modify the model.

This class cannot inject default values to models that weren't created by Eloquent (like using new static). But still can trigger the save event. You will have to handle this by yourself.

Example

(new InjectModelAttribute(User::class))
    ->attribute('money', function(User $user) use ($economy) {
        return $economy->getMoney($user);
    }, function (User $user, $name, $value) use ($economy) {
        $originalMoney = $economy->getMoney($user);
        $delta = $value - $originalMoney;
        $economy->addMoney($user, $delta);
    });

// Then you can do...
$money = $user->money;
$user->money = $money + 100;