[英]Laravel Eloquent add a prefix to an attribute
我的桌子上有我所在地區的地區名稱。 在每個名字之前添加“區域”字符串是否有一種簡單易行的方法?
有我的表結構:
+------------------------+
| id | name |
+----+-------------------+
| 1 | Sofia |
+----+-------------------+
| 2 | Shumen |
+----+-------------------+
| 3 | Varna |
+------------------------+
當我打電話給District :: find(1) - > name來獲得'District Shumen'時我需要。
您可以為其創建屬性訪問器。 在你的模型中:
public function getNameAttribute(){
return 'District ' . $this->attributes['name'];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.