gpt4 book ai didi

laravel - Laravel 不允许更新属性

转载 作者:行者123 更新时间:2023-12-02 10:56:32 25 4
gpt4 key购买 nike

在 Laravel 中,当向 $fillable 添加属性时,我们可以在创建/更新中批量分配,但我想允许属性在创建中批量分配,但不允许更新。现在我使用更新事件来实现这一点。有更好的办法吗?

protected static function boot()
{
static::updating(function ($product) {
$product->price = $product->getOriginal('price');
}
}

最佳答案

我会推荐Obeserver。因为它为您提供了更大的灵 active 。

<?php

namespace App\Observers;

use App\User;

class UserObserver
{

/**
* Listen to the User updating event.
*
* @param \App\User $user
* @return void
*/
public function updating(User $user)
{
if($user->isDirty(['price'])){
// it is updated return false.
}

}
}

关于laravel - Laravel 不允许更新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50483201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com