簡體   English   中英

我想在Laravel中為模型添加自定義屬性

[英]I want to add a custom attribute to model in laravel

我需要在不使用get Attribute的情況下為我的laravel模型添加一個額外的屬性。 我想以默認和自定義格式獲取created_at屬性。

default : created_at : 2018-07-25 15:38:56

second: 10 Days Ago 

如果我使用getCreatedAtAttribute(),它將更改默認屬性。

在您的模型中,添加以下內容:

protected $appends = ['readable_created_at'];

public function getReadableCreatedAtAttribute()
{
    return $this->created_at; //or however you want to manipulate it
}

之后,您可以像平常一樣訪問它,例如$user->readable_created_at

在此處閱讀Laravel Doc的更多內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM