Capitano
Capitano

Reputation: 1

Laravel 5 - How to make a relationship between 3 tables?

I have 3 tables in my database with the following structure:

'projects'
-> id
-> name

'tasklists'
-> id
-> project_id

'tasks'
-> id
-> project_id
-> tasklist_id

I made for each table a model.

- Project.php
- Tasklist.php
- Task.php

My goal is to print in my view each tasklist with all the tasks for a specific project.

So as an example:

Project name: Stackoverflow
-> Tasklist #1
   -> Task #1
   -> Task #2
   -> Task #3
-> Tasklist #2
   -> Task #4
   -> Task #5
-> Tasklist #3
   -> Task #6

I have a little bit of knowledge about the simple relationships in Laravel between two tables. But I can't figure out how to make use of the relationships with 3 tables.

Can someone give me some tips/hints? Which relationship type is the best to use? How do I write this out and in which model should I place this? I know the logic inside my head but I don't know how to write it out in code.


Edit -> This is what I have now.

Tasklist

public function projects()
  {
    return $this->belongsTo('App\Project');
  }

  public function tasks()
  {
    return $this->hasMany('App\Task', 'tasklist_id');
  }

Task.php

public function tasks()
      {
        return $this->belongsTo('App\Tasklists');
      }

Project model

public function Tasklists()
  {
    return $this->hasManyThrough(
      'App\Task',
      'App\Tasklists',
      'project_id', 
      'tasklist_id',
      'id',
      'id'
    );
  }

Now when I print dd(Project::first()->tasklists);

I get all the tasks from my database (not sorted on project_id or tasklist_id). So I guess I'm closer but not there yet.

Kinds regards,

Dylan

Upvotes: 0

Views: 5825

Answers (3)

AndyChern
AndyChern

Reputation: 336

No need for hasManyThrough -- You should be able to use your existing relationships like this ...

Project.php ...

function taskLists(){
    return $this->hasMany('App\TaskList');
}

TaskList.php

function tasks(){
    return $this->hasMany('App\Tasks');
}

And then in your controller ...

Projects::with('taskLists', 'taskLists.tasks')->get();

That will return all of the projects with the task lists as a relationship. Then on the lists, the tasks as a relationship to those.

To print, you can do something like ...

@foreach($projects as $project)
    <h1>Project Name: {{$project->name}}</h1>
    @foreach($project->taskLists as $list)
        <h4>{{$list->name}}</h4>
        <ul>
            @foreach($list->tasks as $task)
                <li>{{$task->name)</li>
            @endforeach
        </ul>
    @endforeach
@endforeach

Upvotes: 1

Devon Bessemer
Devon Bessemer

Reputation: 35337

Tasklists belong to Projects, Projects have many tasklists.

Tasks belong to Tasklists, Tasklists have many tasks.

Projects have many tasks through tasklists.

These descriptions should line up with what Laravel calls the relationship methods.

Upvotes: 0

Afraz Ahmad
Afraz Ahmad

Reputation: 5386

Use Many to Many Relationship To do so you will create 3 tables:

  1. projects
  2. tasks
  3. project_task (this is pivot table which holds information about relations)

To learn more about it read this official document https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

Upvotes: 0

Related Questions