How to set Eloquent relationship belongsTo THROUGH another model in Laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Establishing Eloquent Relationship BelongsTo THROUGH Another Model in Laravel Introduction: Setting up relationships between models is an important aspect of building complex web applications. In Laravel, managing these relationships can be done using Eloquent ORM. This blog post will guide you on how to create a belongsTo relationship that inherits through another model (in this case, Listing). We will also provide a practical example and some best practices to ensure proper establishment of the association between models, manufacturers, listings, and their relationships. Step 1: Understanding Relationship Types in Laravel Before proceeding with writing codes, let's understand the different types of relationship Eloquent can handle. There are four major ones: one-to-one, one-to-many, many-to-many, and has-many-through relations. In this case, we will be focusing on "belongsTo" - a basic one-to-one relation between the children model (Listing) and its parent (Manufacturer). Step 2: Adding Relationship in Listing Model First, we need to add the belongsTo relationship in the Listing model. This will indicate that each Listing belongs to a specific Manufacturer. The code for this can be written as follows:
public function manufacturer()
{
    return $this->belongsTo('Manufacturer', 'model_id');
}
This relationship will fetch the manufacturer associated with each Listing by using its 'model_id' key. For example, if a Listing is created for a given Model that belongs to a Manufacturer, this relationship will help you retrieve the corresponding manufacturer. Step 3: Adding Relationships in Manufacturer and Model Models Next, we need to create relationships between manufacturers (one-to-many) and the models they have. This can be done by updating the Manufacturer model as follows:
public function listings()
{
    return $this->hasManyThrough('Listing', 'Model', 'manufacturer_id', 'model_id');
}

public function models()
{
    return $this->hasMany('Model', 'manufacturer_id');
}
This allows us to find all the listings associated with a given manufacturer and also retrieve a collection of their respective models. Step 4: Testing Relationships in Your Application Now, we need to test these relationships in our application to make sure they are properly set up. In your controller or view file, you can use the following code snippet:
$listing = Listing::find(1);
echo $listing->model->name; // Outputs 'Model Name' (as it belongsTo 'Model')
echo $listing->manufacturer()->name; // Throws an error (as the relationship is not set up properly)
echo $listing->manufacturer->name; // Also throws an error because we need to go through the models relation first.

$revised_listing = Listing::find(1);
echo $revised_listing->manufacturer() == $manufacturer_object
    ->join('models', 'models.id', '=', 'listings.model_id')
    ->where('models.manufacturer_id', '=', $manufacturer_id) // Replace with the actual manufacturer id you need to retrieve.
    ->get(); // This query will ensure you get the proper manufacturers associated with the listings.
Step 5: Conclusion In this blog post, we have discussed how to set up Eloquent relationships in Laravel when inheriting through another model. We created a belongsTo relationship between Listing and Manufacturer using their ID keys and further established the association with the use of hasManyThrough and hasMany relationships for models. Finally, we tested these relationships in our application to ensure they are working as expected. By following these steps, you can successfully manage complex parent-child relationships between your models effectively.