私はLaravelでカスタムModelメソッドを定義しようとしています。私はSubscription
とNotification
との間にn:m関係を持っています。SubscriptionNotification
を超えています。Laravelでリレーションを取得するカスタムモデルメソッド
私はすでにデフォルトの関係を定義した:
public function subscription_notifications() {
return $this->hasMany('App\SubscriptionNotification');
}
public function notifications() {
return $this->belongsToMany('App\Notification', 'subscription_notifications');
}
は、今私は、通知のコレクションを返すメソッドを定義します。私は
[LogicException]
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
:私は、配列に私が欲しいの通知のIDを収集し、以下の方法で書きます。
public function notifications_due() {
// Collect $notification_ids
return $this->belongsToMany('App\Notification', 'subscription_notifications')->whereIn('notifications.id', $notification_ids)->get();
}
をしかし、私は$subscription->notifications_due
によって優先mothodを使用したいとき、私は次のエラーを取得します私はLaravelに新しい(私はRailsから来た)。これがLaravelでも可能かどうかはわかりません。たぶん誰かが私を助けることができます。ありがとう!
感謝を!それはそれだった。 – mgluesenkamp