简体   繁体   English

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

[英]Laravel 5.3 - Paginate notifications and subnotifications once?

I can paginate notifications and subnotifications for user notifiable_id 5 individually without any issues. 我可以分页通知和subnotifications用户notifiable_id 5独立没有任何问题。 However, I am trying to have the results paginated together in one instance. 但是,我试图一次将结果分页。

1) DB table name/data 1)数据库表名称/数据

notifications

在此处输入图片说明

subnotifications

在此处输入图片说明

2) Pagination 2)分页

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): 我需要能够将它们合并以仅返回一个既具有通知又具有子通知的paginate(10)实例,因此例如(伪代码)这样​​的事情:

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

How can this be done with one pagination instance efficiently? 如何使用一个分页实例有效地完成此操作?

Update 1: 更新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. notifications表中UserWasFollowed类型的notifications

b. Subnotifications from subnotifications table with the related notification from notifications table. 从Subnotifications subnotifications表与相关的通知notifications表。

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

Having not much information about the purpose of the subnotification table or what it does, you can try the following method : 没有太多有关子通知表的用途或其用途的信息,可以尝试以下方法:

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

Note Talking about efficiency, the intent of subnotification must be known, why do you need it and how are you going to use the data retrieved by your second query. 注意:关于效率,必须知道子subnotification的意图,为什么需要它以及如何使用第二个查询检索到的数据。 Having an answer to that might make a difference to $collection 对此有一个答案可能会对$collection有所帮助


EDIT 编辑
The simple way I can think of, is using a closure within your eager loading with Like so : 我能想到的简单方法是,在您渴望加载时with Like这样的闭包:

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

You can learn more about it at Laravel Eloquent Relationships under Constraining Eager Loads 您可以在约束急切负载下的Laravel雄辩关系中了解更多信息


UPDATE 2 更新2
try this 尝试这个

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

Using this with a similar setup worked fine for me. 将它与类似的设置配合使用对我来说效果很好。
Note Be sure to change the relations names to their proper ones if they don't match 注意如果不匹配,请确保将关系名称更改为正确的名称

UPDATE 3 更新3
When using your exact same setup for subnotifications provided in the related question, with the following queries : 当对相关问题中提供的子通知使用完全相同的设置时,请使用以下查询:

Notifications\\TestNotification.php is similar to the SomethingCoolHappen.php in the example. Notifications\\TestNotification.php与示例中的SomethingCoolHappen.php类似。

The model, channel and migrations are the same. 模型,渠道和迁移是相同的。 So you can use them as they are. 因此,您可以按原样使用它们。
What I did to get what you wanted is the following : 我为得到您想要的东西是:

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM