Php Laravel手动更新时间戳返回null
我和一些孩子有个提议 我想在每个孩子的父母expire\u时间字段更改时更新他们的expire\u时间字段 但它返回null expire\u时间类型为时间戳 下面是我在模型中的代码:Php Laravel手动更新时间戳返回null,php,mysql,laravel,model,timestamp,Php,Mysql,Laravel,Model,Timestamp,我和一些孩子有个提议 我想在每个孩子的父母expire\u时间字段更改时更新他们的expire\u时间字段 但它返回null expire\u时间类型为时间戳 下面是我在模型中的代码: if ($offer->childrenOffers->count()) { $offer->childrenOffers->each(function (self $childOffer) use($offer) { ddd($offer->
if ($offer->childrenOffers->count()) {
$offer->childrenOffers->each(function (self $childOffer) use($offer) {
ddd($offer->expire_time); //returns "2018-02-30 23:59:59"
ddd($childOffer->expire_time); //returns "2018-02-01 23:59:59"
$childOffer->expire_time = $offer->expire_time;
dd($childOffer->expire_time); //returns null
$childOffer->save();
});
}
我怎么能这么做 将
expire\u time
添加到模型中的可填充字段,首先确保保存到数据库,然后检查
if ($offer->childrenOffers->count()) {
$offer->childrenOffers->each(function (self $childOffer) use($offer) {
$childOffer->expire_time = $offer->expire_time;
$childOffer->save();
dd($childOffer->expire_time); //returns null
});
}
使用
保存的
或更新的
每次更新模型时,都可以更新其子项。您可以使用
雄辩的模型触发多个事件,使您能够连接到模型生命周期中的以下几点:检索、创建、创建、更新、更新、保存、保存、删除、删除、恢复、还原。事件允许您在每次数据库中保存或更新特定模型类时轻松执行代码
在您的Offer
模型中,您可以添加引导方法,以便在保存或更新Offer时自动更新子项,如下所示:
public static function boot() {
parent::boot();
//When the offer is updated
static::updated(function ($offer) {
$offer->childrenOffers->each(function ($child) use ($offer){
$child->expire_time = $offer->expire_time;
$child->save();
});
});
}
调用save之前,
$childOffer->expire\u time
不会更新。代码中似乎没有任何错误。问题在于expire\u time类型,如果我将数据传递给所有字段,则所有字段都会更改,除了此字段@sohel0415是否有任何错误??否:(@sohel0415它位于可填充字段中。如果我向所有字段传递数据,所有字段都会更改,除了此字段!!我真的不知道为什么:(我尝试了所有这些,但都不起作用。)(问题不在于保存或更新……问题在于该字段类型。