angelorigo's avatar

Relate two more tables

Hi, using Eloquent, how can i relate two more models just to bring one colum from model CityPopulation ?

I have a model Posts, inside there is a method using a belongTo to relate to a model Author passing the name of the model (Author) and the column criteria to join the two tables.

How can i relate this model Author to another two more tables AuthorCity and CityPopulation just to bring one column from model CityPopulation (numberPopulation) into the same results from the BelongsTo method into model Posts ?

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Posts extends Model {

public function author()
{

    return $this->belongsTo(Author::class, 'author_id', 'id');

}

}

1 like
2 replies
LaryAI's avatar
Level 58

To achieve this using Eloquent, you can utilize relationships to join multiple tables and retrieve the desired column from the CityPopulation model. Here's a step-by-step guide on how you can set this up:

  1. Define Relationships in Your Models:

    • Author Model: Define a relationship to the AuthorCity model.
    • AuthorCity Model: Define a relationship to the CityPopulation model.
  2. Use Eager Loading in the Posts Model:

    • Use Eloquent's eager loading to fetch the related data.

Here's how you can set up these relationships:

Step 1: Define Relationships

Author Model:

class Author extends Model
{
    public function authorCity()
    {
        return $this->hasOne(AuthorCity::class);
    }
}

AuthorCity Model:

class AuthorCity extends Model
{
    public function cityPopulation()
    {
        return $this->belongsTo(CityPopulation::class);
    }
}

Step 2: Eager Load in the Posts Model

In your Posts model or wherever you are querying the Posts, you can use eager loading to bring in the numberPopulation from the CityPopulation model:

$posts = Post::with(['author.authorCity.cityPopulation'])->get();

foreach ($posts as $post) {
    $numberPopulation = $post->author->authorCity->cityPopulation->numberPopulation ?? 'N/A';
    echo "Post ID: {$post->id}, Population: {$numberPopulation}\n";
}

Explanation:

  • with(['author.authorCity.cityPopulation']): This line tells Eloquent to load the author relationship on the Post model, then the authorCity relationship on the Author model, and finally the cityPopulation relationship on the AuthorCity model.
  • numberPopulation: Access the numberPopulation column from the CityPopulation model.

This setup assumes that your database relationships are correctly defined with foreign keys. Adjust the relationship methods if your table and column names differ.

vincent15000's avatar

When you have many relationships like in your case, sometimes the better solution is to create a database view with all needed fields from the different tables and retrieve the datas from the view.

Then you can create a model for the database view and handle this model as any other model.

1 like

Please or to participate in this conversation.