As a web developer, I know how crucial it is to establish efficient and robust database relationships when building dynamic and data-driven applications. Laravel, my go-to PHP framework, has consistently impressed me with its powerful tools and features for handling complex relationships between database tables.
One such feature is the "Has Many Through" relationship, which enables me to navigate through intermediate models and create meaningful connections between different data entities.
In this article, I will take you on a journey to explore the Laravel 10 Has Many Through relationship and demonstrate how we can use it to establish a seamless association between three fundamental database tables: "categories," "orders," and "products."
Understanding and mastering this relationship is vital for any developer aiming to build sophisticated applications with structured and interconnected data.
Throughout this article, I'll guide you through the process of setting up the necessary database tables, defining eloquent models, and configuring the Has Many Through relationship.
To make things more tangible, I'll provide real-world examples to illustrate how this relationship can be applied to practical scenarios.
Whether you are a seasoned Laravel developer eager to expand your knowledge or a newcomer excited to grasp the power of Laravel's eloquent relationships, I'm confident that this article will serve as an insightful guide.
So, let's dive in together and uncover the magic of the Laravel 10 Has Many Through relationship.
For example, a category is connected with products, and products are connected with orders, then we can access all orders connected with specific categories. So, simply you can access or get data using intermediate model relation using hasManyThrough in laravel 10.
Now, we will create categories, products, and order tables. categories table connected with products and products table connected with orders table like the below screenshot and we also create migration and model for all tables and retrieve data using the model.
Create a migration file for the "categories" table using the following command.
php artisan make:migration create_categories_table
Then, open the generated migration file located in the "database/migrations" directory and define the schema for the "categories" table.
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Create a migration file for the "products" table using the following command.
php artisan make:migration create_products_table
Then, open the generated migration file and define the schema for the "products" table.
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('categories_id')->unsigned();
$table->timestamps();
$table->foreign('categories_id')->references('id')->on('categories')->onDelete('cascade');
});
Create a migration file for the "orders" table using the following command.
php artisan make:migration create_orders_table
Then, open the generated migration file and define the schema for the "orders" table.
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});
Category Model:
Create a new model file for the "Category" model using the following command.
php artisan make:model Category
Open the generated "Category" model file located in the "app/Models" directory and define the model along with the Has Many Through relationship.
<?php
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
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 $categories->orders like this.
Laravel is now aware of the connections between the "categories," "orders," and "products" tables. You can now perform queries and eager loading to retrieve related data seamlessly.
To retrieve records using the Has Many Through relationship, you can perform queries on the "Category" model and use the relationship method "products()" to access the associated products through the "Order" model.
$category = Category::find(1);
dd($category->order);
You might also like:
- Read Also: Laravel 10 one to many Relationship Example
- Read Also: Laravel 10 one to one Relationship Example
- Read Also: How To Create CRUD Operation In Laravel 10 Livewire
- Read Also: How To Integrate Paypal Payment Gateway In Laravel 10