Laravel 5.3 - 通知和子通知分页一次?

Laravel 5.3 - Paginate notifications and subnotifications once?

我可以单独为用户 notifiable_id 5 分页通知和子通知,没有任何问题。但是,我试图在一个实例中将结果分页在一起。

1) DB table name/data

notifications

subnotifications

2) 分页

我可以像这样对每个单独的关系进行分页:

$notifs = $user->notifications()->where('type', 'UserWasFollowed')->paginate(10);
$subnotifications = $user->subnotifications()->with('notification')->paginate(10);

我需要能够合并它们以仅取回一个具有通知和子通知的 paginate(10) 实例,例如这样的东西(伪代码):

$allNotifs = $user->(notifications()->where('type', 'UserWasFollowed'))
                  ->(subnotifications()->with('notification'))
                  ->paginate(10);

如何使用一个分页实例有效地完成这项工作?

更新 1:

用户模型

class User extends Authenticatable {
    use Notifiable;
    use HasSubnotifications;
}

子通知模型

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();
    }
}

查询用户的:

一个。来自 notifications table.

UserWasFollowed 类型的通知

b。来自 subnotifications table 的子通知以及来自 notifications table.

的相关通知
$allNotifications = $user->subnotifications()->whereHas('notifications',function($query){
  $query->where('type', 'UserWasFollowed');
})->with('notification')->get();

没有太多关于子通知的目的 table 或它做什么的信息,您可以尝试以下方法:

public function your_method_name()
{
    /* IMPORTANT : Purpose of subnotification must be known to have a more structured query */
    $collection = $user->notifications()->where('type', 'UserWasFollowed')->get()->merge($user->subnotifications()->with('notification')->get());

    $currentPage = LengthAwarePaginator::resolveCurrentPage();

    $perPage = 10;

    $currentPageData = $collection->slice($currentPage * $perPage, $perPage)->all();

    $paginatedFinalCollection = new LengthAwarePaginator($currentPageData, count($collection), $perPage);

    return dd($paginatedFinalCollection);
}

注意 说到效率,必须知道subnotification的意图,你为什么需要它,你将如何使用你的第二个检索到的数据询问。得到答案可能会对 $collection

产生影响

编辑
我能想到的简单方法是在您的预加载中使用闭包 with 像这样:

$sn = $user->subnotifications()->with(['notification'=>function($query){
      $query->where('type','UserWasFollowed');
}])->paginate(10);

您可以在 Laravel Eloquent RelationshipsConstraining Eager Loads

中了解更多信息

更新 2
试试这个

$user->subnotifications()->whereHas('notifications',function($query){
    $query->where('notification_type','UserWasFollowed');
})->with('notifications')->get();

将它与类似的设置一起使用对我来说效果很好。
注意如果关系名称不匹配,请务必将关系名称更改为正确的名称

更新 3
当对相关问题中提供的子通知使用完全相同的设置时,使用以下查询:

Notifications\TestNotification.php 类似于示例中的 SomethingCoolHappen.php

模型、通道和迁移是相同的。所以你可以按原样使用它们。
为了得到你想要的,我所做的如下:

Route::get('/step1', function () {
    // example - my followers
    $followers = App\User::first();
    // notify them
    $x = Notification::send($followers, new TestNotification(['arg1' => 1, 'arg2' => 2]));
    dd($x);
});

Route::get('/step2', function () {
    // my follower
    $user = App\User::find(1);

    $subnotifications = $user->subnotifications()->whereHas('notification',function($query){$query->where('type','App\Notifications\TestNotification');})->with('notification')->get();

//this gives you the subnotifications collection with the notification included
dd($subnotifications);

//get the notification linked to one entry of the collection
dd($subnotifications->first()->notification()->first());

});