例如,创建模型后,我不希望任何人能够再次更新该记录。相反,它应该被全新的记录覆盖并存档。
这是一个简单的特征,您可以在模型上使用它来禁用更新:
trait PreventsUpdating
{
    public static function bootPreventsUpdating()
    {
        static::updating(function (Model $model) {
            return false;
        });
    }
} 
只需在您的模型上使用它,您将无法再更新它。
#改进
我们可以更进一步,使其更具可重用性和 DRY-er。
trait PreventsModelEvents
{
    public static function bootPreventsModelEvents()
    {
        foreach (static::$prevents as $event) {
            static::{$event}(function (Model $model) {
                return false;
            });
        }
    }
} 
现在,当我们想在模型上使用它时,我们可以这样做:
class User extends Model
{
    use PreventsModelEvents;
    protected static $prevents = ['updating'];
} 
当我们尝试更新User模型时,它将停止并返回 false。这也可以应用于其他事件,例如saving和creating。








