Laravel 5.3 - 分页通知和次级通知一次? [英] Laravel 5.3 - Paginate notifications and subnotifications once?
问题描述
我可以分别为用户 notifiable_id
5分页通知和子通知,没有任何问题。但是,我想在一个实例中将结果分组在一起。
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)数据库表名/数据
通知
subnotifications
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);
我需要能够合并它们才能收回一个 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?
更新1:
用户模型 / p>
User Model
class User extends Authenticatable {
use Notifiable;
use HasSubnotifications;
}
Subnotification模型
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。 UserWasFollowed
的通知来自通知
表。
a. Notifications of UserWasFollowed
type from notifications
table.
b 。 subnotifications
表格中的细节来自通知的相关通知
表。
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();
推荐答案
没有关于子站点表的目的的很多信息或者它做什么,你可以尝试以下方法:
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);
}
注意说到效率, code> subnotification 必须知道,为什么需要它,以及如何使用第二个查询检索到的数据。有一个答案可能会使 $ collection
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. Having an answer to that might make a difference to $collection
编辑
我可以想到的简单方法是在您急于加载之前使用一个关闭
像这样:
EDIT
The simple way I can think of, is using a closure within your eager loading with
Like so :
$sn = $user->subnotifications()->with(['notification'=>function($query){
$query->where('type','UserWasFollowed');
}])->paginate(10);
您可以在 Laravel雄辩的关系 限制渴望的负载
更新2
尝试此
UPDATE 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
更新3
当使用完全相同的设置进行相关问题中提供的子操作时,请执行以下查询:
UPDATE 3
When using your exact same setup for subnotifications provided in the related question, with the following queries :
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());
});
这篇关于Laravel 5.3 - 分页通知和次级通知一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!