一个很简单的laravel代码:
User::where('id',1)->update(['last_login'=>time()]);
/**
* Update a record in the database.
*
* @param array $values
* @return int
*/
public function update(array $values)
{
return $this->toBase()->update($this->addUpdatedAtColumn($values));
}
When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.
You need to retrieve the user from the database and then save that user in order to fire the event. For example:
This will NOT fire the update event:
User::where('id', $id)->update(['username' => $newUsername]);
This will fire the update event:
User::find($id)->update(['username' => $newUsername]);
/**
* Log a query in the connection's query log.
*
* @param string $query
* @param array $bindings
* @param float|null $time
* @return void
*/
public function logQuery($query, $bindings, $time = null)
{
if (isset($this->events)) {
$this->events->fire(new Events\QueryExecuted(
$query, $bindings, $time, $this
));
}
if ($this->loggingQueries) {
$this->queryLog[] = compact('query', 'bindings', 'time');
}
}
/**
* Register a database query listener with the connection.
*
* @param \Closure $callback
* @return void
*/
public function listen(Closure $callback)
{
if (isset($this->events)) {
$this->events->listen(Events\QueryExecuted::class, $callback);
}
}
/**
* Fire an event for this connection.
*
* @param string $event
* @return void
*/
protected function fireConnectionEvent($event)
{
if (! isset($this->events)) {
return;
}
switch ($event) {
case 'beganTransaction':
return $this->events->fire(new Events\TransactionBeginning($this));
case 'committed':
return $this->events->fire(new Events\TransactionCommitted($this));
case 'rollingBack':
return $this->events->fire(new Events\TransactionRolledBack($this));
}
}