gpt4 book ai didi

Laravel-创建模型启动不触发

转载 作者:行者123 更新时间:2023-12-03 15:46:18 24 4
gpt4 key购买 nike

我希望在$protected $foo事件中为'foo'分配static::creating,但是当我输出最终对象时,该属性为null。该事件似乎没有触发:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model {

protected $foo;

public static function boot() {
parent::boot();
static::creating(function (Item $item) {
echo "successfully fired"; //this does not get echoed
$item->foo = 'foo';
});
}
}

我究竟做错了什么?

最佳答案

您的代码运行正常,我以此方式进行了最终测试

在模型中

protected $foo;

public static function boot() {
parent::boot();

//while creating/inserting item into db
static::creating(function (AccountCreation $item) {
echo "successfully fired";
$item->foo = 'fooasdfasdfad'; //assigning value
});

//once created/inserted successfully this method fired, so I tested foo
static::created(function (AccountCreation $item) {
echo "<br /> {$item->foo} ===== <br /> successfully fired created";
});
}

Controller 中的
AccountCreation::create(['by'=>'as','to'=>'fff','hash'=>'aasss']);

浏览器中的,收到此响应
successfully fired
fooasdfasdfad =====
successfully fired created

关于Laravel-创建模型启动不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35907641/

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