From e8c5a1ddb35d2de6d1ee1c973efbdd99d0b87171 Mon Sep 17 00:00:00 2001 From: wizston Date: Fri, 12 May 2017 15:14:49 +0100 Subject: [PATCH 1/2] Add config option for model owner key --- src/Notifynder/Collections/Config.php | 8 ++++++++ src/Notifynder/Contracts/ConfigContract.php | 5 +++++ src/Notifynder/Models/Notification.php | 4 ++-- src/config/notifynder.php | 7 +++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Notifynder/Collections/Config.php b/src/Notifynder/Collections/Config.php index db5752f..16de551 100644 --- a/src/Notifynder/Collections/Config.php +++ b/src/Notifynder/Collections/Config.php @@ -61,6 +61,14 @@ public function getNotifiedModel() throw new InvalidArgumentException("The model class [{$class}] doesn't exist."); } + /** + * @return string + */ + public function getNotifiedModelOwnerKey() + { + return (string) $this->get('model_owner_key', 'id'); + } + /** * @return array */ diff --git a/src/Notifynder/Contracts/ConfigContract.php b/src/Notifynder/Contracts/ConfigContract.php index a1b7580..5380b9c 100644 --- a/src/Notifynder/Contracts/ConfigContract.php +++ b/src/Notifynder/Contracts/ConfigContract.php @@ -27,6 +27,11 @@ public function isTranslated(); */ public function getNotifiedModel(); + /** + * @return string + */ + public function getNotifiedModelOwnerKey(); + /** * @return array */ diff --git a/src/Notifynder/Models/Notification.php b/src/Notifynder/Models/Notification.php index b08e72c..4c73300 100644 --- a/src/Notifynder/Models/Notification.php +++ b/src/Notifynder/Models/Notification.php @@ -91,7 +91,7 @@ public function from() return $this->morphTo('from'); } - return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'from_id'); + return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'from_id', notifynder_config()->getNotifiedModelOwnerKey()); } /** @@ -103,7 +103,7 @@ public function to() return $this->morphTo('to'); } - return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'to_id'); + return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'to_id', notifynder_config()->getNotifiedModelOwnerKey()); } /** diff --git a/src/config/notifynder.php b/src/config/notifynder.php index 731c5e3..182389f 100644 --- a/src/config/notifynder.php +++ b/src/config/notifynder.php @@ -15,6 +15,13 @@ */ 'model' => 'App\User', + /* + * If your model is not using id as it's primary key + * please specific it here, this option is not + * considerate if using notifynder as polymorphic + */ + 'model_owner_key' => 'id', + /* * Do you want have notifynder that work polymorphically? * just swap the value to true and you will able to use it! From 8767a15d6b99a250733dde7c859e7c25fc15d664 Mon Sep 17 00:00:00 2001 From: wizston Date: Sun, 14 May 2017 13:05:44 +0100 Subject: [PATCH 2/2] Change method of getting owner primary key to laravel native helper function as suggested by @Gummibeer --- src/Notifynder/Collections/Config.php | 8 -------- src/Notifynder/Contracts/ConfigContract.php | 5 ----- src/Notifynder/Models/Notification.php | 11 +++++++++-- src/config/notifynder.php | 7 ------- 4 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/Notifynder/Collections/Config.php b/src/Notifynder/Collections/Config.php index 16de551..db5752f 100644 --- a/src/Notifynder/Collections/Config.php +++ b/src/Notifynder/Collections/Config.php @@ -61,14 +61,6 @@ public function getNotifiedModel() throw new InvalidArgumentException("The model class [{$class}] doesn't exist."); } - /** - * @return string - */ - public function getNotifiedModelOwnerKey() - { - return (string) $this->get('model_owner_key', 'id'); - } - /** * @return array */ diff --git a/src/Notifynder/Contracts/ConfigContract.php b/src/Notifynder/Contracts/ConfigContract.php index 5380b9c..a1b7580 100644 --- a/src/Notifynder/Contracts/ConfigContract.php +++ b/src/Notifynder/Contracts/ConfigContract.php @@ -27,11 +27,6 @@ public function isTranslated(); */ public function getNotifiedModel(); - /** - * @return string - */ - public function getNotifiedModelOwnerKey(); - /** * @return array */ diff --git a/src/Notifynder/Models/Notification.php b/src/Notifynder/Models/Notification.php index 4c73300..380ca3c 100644 --- a/src/Notifynder/Models/Notification.php +++ b/src/Notifynder/Models/Notification.php @@ -53,6 +53,7 @@ class Notification extends Model 'extra' => 'array', ]; + protected $notifiedModelInstance; /** * Notification constructor. * @@ -72,6 +73,10 @@ public function __construct($attributes = []) } parent::__construct($attributes); + + + $notifiedModel= notifynder_config()->getNotifiedModel(); + $this->notifiedModelInstance = new $notifiedModel; } /** @@ -87,11 +92,13 @@ public function category() */ public function from() { + + if (notifynder_config()->isPolymorphic()) { return $this->morphTo('from'); } - return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'from_id', notifynder_config()->getNotifiedModelOwnerKey()); + return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'from_id', $this->notifiedModelInstance->getKeyName()); } /** @@ -103,7 +110,7 @@ public function to() return $this->morphTo('to'); } - return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'to_id', notifynder_config()->getNotifiedModelOwnerKey()); + return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'to_id', $this->notifiedModelInstance->getKeyName()); } /** diff --git a/src/config/notifynder.php b/src/config/notifynder.php index 182389f..731c5e3 100644 --- a/src/config/notifynder.php +++ b/src/config/notifynder.php @@ -15,13 +15,6 @@ */ 'model' => 'App\User', - /* - * If your model is not using id as it's primary key - * please specific it here, this option is not - * considerate if using notifynder as polymorphic - */ - 'model_owner_key' => 'id', - /* * Do you want have notifynder that work polymorphically? * just swap the value to true and you will able to use it!