2017-08-17 113 views
0

我想自定義Laravel的模型類來記錄一些其他的事情(例如誰創建/更新/刪除該行中的DBLaravel:自定義模型類

我看到這個模型類的582-。 587

// First we need to create a fresh query instance and touch the creation and 
// update timestamp on the model which are maintained by us for developer 
// convenience. Then we will just continue saving the model instances. 
if ($this->usesTimestamps()) { 
    $this->updateTimestamps(); 
} 

使用這個(問題/ HasTimestamps.php 32-48):

/** 
* Update the creation and update timestamps. 
* 
* @return void 
*/ 
protected function updateTimestamps() 
{ 
    $time = $this->freshTimestamp(); 

    if (! $this->isDirty(static::UPDATED_AT)) { 
     $this->setUpdatedAt($time); 
    } 

    if (! $this->exists && ! $this->isDirty(static::CREATED_AT)) { 
     $this->setCreatedAt($time); 
    } 
} 

我的問題:如何重寫也記錄了 「誰」,而不在供應商文件夾修修補補? (誰來fr om Auth::user()->id

回答

2

你在找什麼叫做Oberserver。它監聽模型的數據庫事件(如創建,刪除,更新...)。

看看Laravel documentation

+0

謝謝,它的工作就像一個魅力:) – Feralheart

0

如何重寫也記錄了「誰」,而不在供應商文件夾修修補補

您應該創建自己的基礎模型類(通常abstract將延長雄辯的基類,然後把所有你額外的代碼,並最終使所有的模型擴展你的基類。