У меня есть модель User
, которая имеет отношение к Notification
.
class User extends Authenticatable
{
protected $with = ['notifications'];
# relationship methods
public function notifications() //my notifications
{
return $this->hasMany(Notification::class, 'subject_id');
}
public function notify() //to create a notifications to other user
{
return $this->hasMany(Notification::class, 'causer_id');
}
# other methods
public function unreadNotifications()
{
return $this->notifications->filter(function($n){
return $n->seen==0;
});
}
public function sendNotification($subject, $data)
{
$this->notify()->create(['subject_id' => $subject->id, 'title' => $data['title'], 'description' => $data['description']]);
}
}
EDITED class Notification extends Model
{
protected $guarded = [];
public function causer()
{
return $this->belongsTo(User::class, 'causer_id');
}
public function subject()
{
return $this->belongsTo(User::class, 'subject_id');
}
}
После того, как я сделаю этот unreadNotifications
метод начинает сбой:
Превышено максимальное время выполнения 60 секунд
Даже при простом вызове $this->notifications
все равно не получается. Кто-нибудь знает, как это исправить?