Laravel 8 Has Many Through Relationship Example

In this article, we will see laravel 8 has many through relationship example. hasManyThrough relationships are difficult to understand compared to other relationships. you use hasManyThrough relationship laravel 6, laravel 7 and laravel 8. The has-many-through relationship provides a convenient way to access distant relations via an intermediate relation.

So, let's see has many through in laravel 8.

For example, if a category is connected with products and products connected with orders, then we can access all orders connected with a specific category. So, simply you can access or get data using intermediate model relation using hasManyThrough in laravel 8.

Now, we will create categories, products, and order tables. categories table connected with products and products table connected with orders table like below screenshot and we also create migration and model for all table and retrieve data using the model.




Create Migration:

Categories Table:

Schema::create('categories', function (Blueprint $table) {





Products Table:

Schema::create('products', function (Blueprint $table) {






Orders Table:

Schema::create('orders', function (Blueprint $table) {







Create Model:

Now, we will create a categories model and define a relationship on the model.

Category Model:


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
    public function order()
        return $this->hasManyThrough(Order::class, Product::class);



The first argument passed to the hasManyThrough the method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.

Though the Order model table does not contain the category_id column, the hasManyThrough relation we can access to $categories->orders like this.


Retrieve Records

Now, retrieve the record using the intermediate model through like the below code example.

$category = Category::find(1);	


You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.