正在更新Eloquent中的自定义时间抽头字段


Updating custom timestap field in Eloquent

我想更新一个自定义时间戳字段,因此编写了这个函数

public function touchDelivery() {
    $this->delivery = $this->freshTimestamp();
    return $this->save();
}
public function pushSuccess($id) {
    return Message::where('id', '=', $id)->touchDelivery();
}

但当执行Laravel抱怨:

调用未定义的方法Illuminate''Database''Query''Builder::touchDelivery()

我猜这段代码在Eloquent文件中。return Message::where('id', '=', $id)->touchDelivery(); 有问题

首先,您需要获取所有消息,然后对于每条消息,您需要调用该方法。像这样的东西。

public function pushSuccess($id) {
   $messages = Message::where('id', '=', $id)->get();
   foreach($messages as $message)
         $message->touchDelivery();
   ...
}

如果这对你有用,请告诉我。