Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.3-将通知和子通知分页一次?_Php_Laravel_Eloquent_Laravel 5.3_Laravel Pagination - Fatal编程技术网

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

Php Laravel 5.3-将通知和子通知分页一次?,php,laravel,eloquent,laravel-5.3,laravel-pagination,Php,Laravel,Eloquent,Laravel 5.3,Laravel Pagination,我可以为用户notifiable\u id5单独分页通知和子通知,没有任何问题。然而,我试图在一个实例中将结果分页在一起 1)数据库表名称/数据 通知 子通知 2)分页 我可以按如下方式对每个关系进行分页: $notifs = $user->notifications()->where('type', 'UserWasFollowed')->paginate(10); $subnotifications = $user->subnotifications()->

我可以为用户
notifiable\u id
5单独分页通知和子通知,没有任何问题。然而,我试图在一个实例中将结果分页在一起

1)数据库表名称/数据

通知

子通知

2)分页

我可以按如下方式对每个关系进行分页:

$notifs = $user->notifications()->where('type', 'UserWasFollowed')->paginate(10);
$subnotifications = $user->subnotifications()->with('notification')->paginate(10);
我需要能够合并它们,以便只返回一个既有通知又有子通知的
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();
    }
}
子通知模型

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();
    }
}
查询用户的:

a。遵循
用户的通知
通知
表中键入

$allNotifications = $user->subnotifications()->whereHas('notifications',function($query){
  $query->where('type', 'UserWasFollowed');
})->with('notification')->get();
b。来自
子通知
表的子通知以及来自
通知
表的相关通知

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

如果没有太多关于子通知表的用途或作用的信息,可以尝试以下方法:

注意谈到效率,必须了解
子通知的意图,为什么需要它,以及如何使用第二次查询检索到的数据。对此有一个答案可能会对
$collection


编辑
我能想到的一个简单方法是,在您的急切加载
中使用闭包
像这样:

您可以在“约束急切负载”下了解更多信息


更新2
试试这个

在类似的设置中使用它对我来说效果很好。
注意:如果不匹配,请确保将关系名称更改为正确的名称

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

Notifications\TestNotification.php
类似于示例中的
somethingcoolcound.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());

});

以下是次通知意图:了解意图后,您对效率有何想法?查询中的任何内容是否会改变以使其成为单个分页器的最有效方式?编辑,测试要求测试编辑,由于语法错误而无法工作,因为
with
应将其内容包含在
[]
中(与您链接的文档相同)。但是,即使使用正确的语法,它也只会拉出带有子通知表字段的
子通知
,并为子通知添加一个
notification
键,该键为
null
(因此不添加通知数据)。它也不会在查询结果中添加
用户的常规
通知
。有没有其他想法可以让它工作?更新。测试与类似的设置,并为我工作。您可以正常访问通知对象。嗯。。。我在问题中添加了一个更新,显示了具有正确关系的代码,但是得到了这个错误
调用undefined method Illumb\\Database\\Query\\Builder::notifications()
我确信它是在错误中引用的whereHas
notifications
。我没有任何方法,因为您只需在用户模型上添加notifiable,当调用
$user->notifications()->where('type','userwasFollowerd')->paginate(10)时,我有并单独工作但使用更新的语法调用时出错。需要查看HasSubnotifications跟踪步骤5,将whereHas和with中的“notification”替换为“notification”。我认为这只是一个命名问题,您在子通知中的关系是通过通知方法而不是通知(打字错误)来表示的。With和WHERHAS采用arg中方法的名称,将两个“通知”替换为“通知”,但返回空数组
[]
,即使所有数据都在那里。不知道为什么用户模型上的通知/子通知如此简单地结合起来会如此复杂,但单独完成时根本没有问题。。另外,如果您查看访问通知下的内容,您会看到您将其称为用户的
通知
,我们只对子通知的相关通知执行
通知
,它应该为空,从屏幕截图判断,您没有键入通知。对于用户,请尝试其他类型。不存在可通知id 5和通知id 1的行
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());

});