如何限制 Laravel 中特定外键值的行数
How to limit the count of rows for a specific foreign key value in Laravel
我们有一个帖子 table 并且 user_id 是外键
例如,我想为这些用户选择帖子
$users=[1,2,13,16,17,19];
$posts = Post::whereIn('user_id', $users)->paginate(10);
但我希望用户 1 和 2 在输出中只有两个帖子,对于其他用户,帖子的数量没有限制。
Note: User 1 and 2 are not always within the $users
array, and due to the condition, one or both of them may not be in the array.
你有解决办法吗?
您可以使用 ->take()
$posts = [];
$users=[1,2,13,16,17,19];
foreach($users as $user)
{
if($user == 1 || $user == 2)
{
$posts[] = Post::where('user_id', $user)->take(2)->get();
}
else
{
$posts[] = Post::where('user_id', $user)->get();
}
}
阅读更多内容
您可以试试这个替代方案:
$posts = [];
$users=[1,2,13,16,17,19];
$userWithJustTwo = [1,2];
$result = array_intersect($users, $userWithJustTwo);
$posts[] = Post::whereIn('user_id', $result)->orderBy('created_at', 'desc')->take(2)->get();
$array = array_diff($users, userWithJustTwo);
$posts[] = Post::whereIn('user_id', $array)->get();
单次查询是做不到的,我们需要像这样分开取
$users=[1,2,13,16,17,19];
// first take all the post except the two
$posts = Post::whereIn('user_id', $users)->whereNotIn('user_id', [1,2])->get()->toArray();
// then take one user 1 post in desc and limit it by 2
$userOnePost = Post::whereIn('user_id', $users)->where('user_id', 1)->limit(2)->orderBy('post.id', 'desc')->get()->toArray();
// then take one user 2 post in desc and limit it by 2
$userTwoPost = Post::whereIn('user_id', $users)->where('user_id', 2)->limit(2)->orderBy('post.id', 'desc')->get()->toArray();
// merge all the array
$allPost = array_merge(posts,$userOnePost,userTwoPost);
试试这个方法:
$users = User::whereNotIn('id', [1,2])->pluck('id');
$posts = [];
$posts[] = Post::whereIn('user_id', [1,2])->take(2)->get();
$posts[] = Post::whereIn('user_id', $users)->get();
如果你想获得最新的 post
请改用这个:
$posts[] = Post::whereIn('user_id', [1,2])->latest('created_at')->take(2)->get();
你也可以像这样使用参数分组
DB::table('post')
->whereIn('user_id', $users)
->where(function ($query) {
$query->whereIn('user_id', [1,2])->limit(2)
->orWhereNotIn('user_id', [1,2]);
})
->get();
我们有一个帖子 table 并且 user_id 是外键 例如,我想为这些用户选择帖子
$users=[1,2,13,16,17,19];
$posts = Post::whereIn('user_id', $users)->paginate(10);
但我希望用户 1 和 2 在输出中只有两个帖子,对于其他用户,帖子的数量没有限制。
Note: User 1 and 2 are not always within the
$users
array, and due to the condition, one or both of them may not be in the array.
你有解决办法吗?
您可以使用 ->take()
$posts = [];
$users=[1,2,13,16,17,19];
foreach($users as $user)
{
if($user == 1 || $user == 2)
{
$posts[] = Post::where('user_id', $user)->take(2)->get();
}
else
{
$posts[] = Post::where('user_id', $user)->get();
}
}
阅读更多内容
您可以试试这个替代方案:
$posts = [];
$users=[1,2,13,16,17,19];
$userWithJustTwo = [1,2];
$result = array_intersect($users, $userWithJustTwo);
$posts[] = Post::whereIn('user_id', $result)->orderBy('created_at', 'desc')->take(2)->get();
$array = array_diff($users, userWithJustTwo);
$posts[] = Post::whereIn('user_id', $array)->get();
单次查询是做不到的,我们需要像这样分开取
$users=[1,2,13,16,17,19];
// first take all the post except the two
$posts = Post::whereIn('user_id', $users)->whereNotIn('user_id', [1,2])->get()->toArray();
// then take one user 1 post in desc and limit it by 2
$userOnePost = Post::whereIn('user_id', $users)->where('user_id', 1)->limit(2)->orderBy('post.id', 'desc')->get()->toArray();
// then take one user 2 post in desc and limit it by 2
$userTwoPost = Post::whereIn('user_id', $users)->where('user_id', 2)->limit(2)->orderBy('post.id', 'desc')->get()->toArray();
// merge all the array
$allPost = array_merge(posts,$userOnePost,userTwoPost);
试试这个方法:
$users = User::whereNotIn('id', [1,2])->pluck('id');
$posts = [];
$posts[] = Post::whereIn('user_id', [1,2])->take(2)->get();
$posts[] = Post::whereIn('user_id', $users)->get();
如果你想获得最新的 post
请改用这个:
$posts[] = Post::whereIn('user_id', [1,2])->latest('created_at')->take(2)->get();
你也可以像这样使用参数分组
DB::table('post')
->whereIn('user_id', $users)
->where(function ($query) {
$query->whereIn('user_id', [1,2])->limit(2)
->orWhereNotIn('user_id', [1,2]);
})
->get();