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

时间:2017-05-12 14:33:08

标签: php laravel eloquent laravel-5.3 laravel-pagination

我可以单独为用户Contact contact = service.get(1); 5分页通知和子通知,而不会出现任何问题。但是,我试图在一个实例中将结果分页。

1)数据库表名称/数据

notifiable_id

enter image description here

notifications

enter image description here

2)分页

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

subnotifications

我需要能够合并它们才能只返回一个同时具有通知和子通知的$notifs = $user->notifications()->where('type', 'UserWasFollowed')->paginate(10); $subnotifications = $user->subnotifications()->with('notification')->paginate(10); 实例,所以像这样的例子(伪代码):

paginate(10)

如何有效地使用一个分页实例?

更新1:

用户模型

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

Subnotification Model

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(); } } 表的UserWasFollowed类型的通知。

notifications表中的子通知以及来自subnotifications表的相关通知。

notifications

1 个答案:

答案 0 :(得分:1)

关于子通知表的用途或其作用的信息不多,您可以尝试以下方法:

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

您可以在{{3>} 约束急切负载

下了解更多相关信息

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

});