How to limit the count of rows for a specific foreign key value in Laravel

How to limit the count of rows for a specific foreign key value in Laravel

We have a posts table and user_id is the foreign key For example, I want to choose posts for these users

We have a posts table and user_id is the foreign key For example, I want to choose posts for these users

$users=[1,2,13,16,17,19];
$posts = Post::whereIn('user_id', $users)->paginate(10);

But I want the user 1 and 2 only have two posts in the output, for the rest of the users there is not a limited number of posts.

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.

Do you have a solution for me?

mysql laravel

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Best MySQL DigitalOcean Performance – ScaleGrid vs. DigitalOcean Managed Databases

Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

How to Hire Laravel Developers and How Much Does It Cost.

Want to make the most out of the latest Laravel technology? Here is your guide on how to hire the best Laravel developer, and at the right price. 

How to perform migration rollback in laravel

Sometimes we need to rollback one specific migration. Here, in this article we will discuss about the migration rollback in laravel.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→