jeudi 26 mars 2020

Accessing a hasMany relation function via Collection

I am trying to get all of the users notifications, and depending on if the user is a buyer or seller (can be both). I have made two functions in my notifications table to filter each other out. My goal is to ultimately run:

$notifications = Auth::user()->notifications()->getBuyerNotifications();

or

$notifications = Auth::user()->notifications()->getSellerNotifications();

I am running into an issue: Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany

User Model:

public function notifications() {
   return $this->hasMany('App\Notification', 'user_id', 'id');
}

Notifications Model:

public function user() {
  return $this->belongsTo('App\User', 'id', 'user_id');
}

public static function getBuyerNotifications() {
  return self::whereNotNull('buyer_id')
              ->whereNull('deleted_at')
              ->get();

}

public static function getSellerNotifications() {
      return $this->whereNotNull('seller_id')
                    ->whereNull('deleted_at')
                    ->get();
}

The command I want to run to get all of the users notifications if they're a buyer: $notifications = Auth::user()->notifications()->getBuyerNotifications();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire