Using Model Relationship in a Single Query

Using Model Relationship in a Single Query

Consider the following:

Consider the following:

$posts = $this->model->newQuery()
    ->whereIn('user_id', $user->following) // specifically this line
    ->orWhere('user_id', $user->id)

The problem with the above is that there are two queries:

  • Get following: $user->following
  • Get posts: Above

This would be much more efficient with the use of a subquery, however, I cannot actually remember the correct way to do it...

I have tried all of the following:

// This was a long-shot...
...->whereIn('user_id', function ($query) use ($user) {

// This works but pretty sure it can be done better with eloquent... ...->whereIn('user_id', function ($query) use ($user) { $query->select('follow_id') ->from('user_follows') ->where('user_id', $user->id); });

Is there a way that this can be achieved by using the previously defined relationship $user->following() instead of manually defining the relationship query like the last example above?


The following relationship is defined as follows:


  • Get the users that the user follows.
  • / public function following() { return $this->belongsToMany('SomeApp\User\Models\User', 'user_follows', 'user_id', 'follow_id')

php 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

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.

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→

Some of the most frequent how tos in Laravel

Spread the loveHow to get relationship from relationship using With() in Laravel Some times there are cases where you want to get relationship from relationship in Laravel, that can be achieved via following: How to create multiple where clauses in…Read More→

10 Laravel Quick Tips

Spread the loveTip 1. Controllers Having Single Action In some situations you need a single action in a controller, if this is the case in Laravel you can achieve it by __invoke() method. Routes: Artisan command to generate this controller:…Read More→

How to Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Laravel is the most popular, free, and open-source PHP framework in the world, known for its expressive and elegant syntax. Laravel is accessible, powerful, and offers some of the best web development tools required for large, robust, and modern applications. In this article, you will learn how to install the Laravel PHP Framework on Ubuntu 20.04 server running on the Nginx web server.