[英]laravel 4.2 model mutators
我目前正在使用通知系統。 我目前遇到的問題是所有通知都是非個人的。
我想要的是將具有正確個性化語言的通知保存到數據庫中,例如,我目前有一條這樣的通知,
Joe Bloggs將Billy Joel添加為項目經理。
我想要的是將通知發送到不是Billy Joel的每個人,但是對於Billy Joel,我希望發送以下內容
Joe Bloggs將您添加為項目經理
目前,我將這樣的通知保存在樞紐分析表的同步中,以供項目經理使用,
$notification = new Notification;
$notification->withURI($organisation->slug);
$notification->withIsNotification(1);
$notification->regarding($organisation);
$notification->user_id = ResourceServer::getOwnerId();
$notification->withType('updated');
//die(print_r($changes));
foreach($changes['attached'] as $k => $v) {
//Loop through each new attachment to the pivot
//look for who the attachement is from the user table and then
//add the final notification details and fire the deliver method
$addedUser = User::find($v);
$addedUserName = $addedUser->first_name . " " . $addedUser->last_name;
$notification->withBody($notifcationSenderName . " added " . $addedUserName . " to the organisation " . $organisation->name);
$notification->deliver($notifyUsers);
}
我想知道當我通過數據庫的模型從數據庫中查詢用戶時,是否可以使用模型更改器來返回用戶的名字和名字,如果不需要個人通知,那么是否可以返回“您”。 增幅劑是否有可能?如果可以,怎么辦?
雖然我不贊成這種方法,但是您肯定可以在訪問器中做到這一點:
public function getNameAttribute()
{
if (Auth::id() == $this->id) return 'you';
return "{$this->first_name} {$this->last_name}";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.