Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

knubbe's avatar
Level 36

Query through two related models

I have 3 tables with following structure:

servers: // Server.php
id, name...
ports: // Port.php
id, server_id, network_id...
networks: // Network.php
id, name...

Is it possible to query all related networks for current server instance?

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Yes, it is possible to query all related networks for a given server instance by using Eloquent relationships in Laravel. You can achieve this by defining the appropriate relationships in your models and then querying through those relationships.

Here’s how you can set up the relationships and perform the query:

  1. Define the relationships in your models:

    • In the Server model, define a relationship to the Port model.
    • In the Port model, define a relationship to the Network model.
  2. Use the relationships to query the related networks for a given server instance.

Here is the code to achieve this:

Server.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Server extends Model
{
    // Define the relationship to the Port model
    public function ports()
    {
        return $this->hasMany(Port::class);
    }

    // Define a relationship to the Network model through the Port model
    public function networks()
    {
        return $this->hasManyThrough(Network::class, Port::class, 'server_id', 'id', 'id', 'network_id');
    }
}

Port.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Port extends Model
{
    // Define the relationship to the Server model
    public function server()
    {
        return $this->belongsTo(Server::class);
    }

    // Define the relationship to the Network model
    public function network()
    {
        return $this->belongsTo(Network::class);
    }
}

Network.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Network extends Model
{
    // Define the relationship to the Port model
    public function ports()
    {
        return $this->hasMany(Port::class);
    }
}
  1. Query the related networks for a given server instance:
// Assuming you have a server instance
$server = Server::find($serverId);

// Get all related networks for the server
$networks = $server->networks;

// Iterate through the networks
foreach ($networks as $network) {
    echo $network->name;
}

In this setup:

  • The Server model has a hasMany relationship with the Port model.
  • The Port model has a belongsTo relationship with both the Server and Network models.
  • The Server model has a hasManyThrough relationship with the Network model through the Port model.

This allows you to easily query all related networks for a given server instance using the networks relationship defined in the Server model.

Chingy's avatar

You are probably looking for hasOneThrough

Please or to participate in this conversation.