I can paginate notifications and subnotifications for user notifiable_id
5 individually without any issues. However, I am trying to have the results paginated together in one instance.
1) DB table name/data
notifications
subnotifications
2) Pagination
I can paginate each relation I have individually like this:
$notifs = $user->notifications()->where('type', 'UserWasFollowed')->paginate(10);
$subnotifications = $user->subnotifications()->with('notification')->paginate(10);
I need to be able to merge them to get back only one paginate(10)
instance which has both notifications and subnotifications, so something like this for example (pseudocode):
$allNotifs = $user->(notifications()->where('type', 'UserWasFollowed'))
->(subnotifications()->with('notification'))
->paginate(10);
How can this be done with one pagination instance efficiently?
Update 1:
User Model
class User extends Authenticatable {
use Notifiable;
use HasSubnotifications;
}
Subnotification Model
class Subnotification extends Model {
protected $table = 'subnotifications';
// set up relation to the parent notification
public function notification() {
return $this->belongsTo(DatabaseNotification::class);
}
// Get the notifiable entity that the notification belongs to.
public function notifiable() {
return $this->morphTo();
}
}
Query for user's:
a. Notifications of UserWasFollowed
type from notifications
table.
b. Subnotifications from subnotifications
table with the related notification from notifications
table.
$allNotifications = $user->subnotifications()->whereHas('notifications',function($query){
$query->where('type', 'UserWasFollowed');
})->with('notification')->get();
[]
even though all the data is there. Not sure why it's so complicated for such a simple joining of both notifications/subnotifications on the user model, but not issues at all when done individually.. Also, if you look at laravel.com/docs/5.4/notifications under Accessing The Notifications, you'll see you call it asnotifications
off the user, we only donotification
for the subnotification's related notification – Wonka