title: How to fetch all record from nested relationship in Laravel
body: <p>I want to fetch records from table Tasks, on the basis of project_id. But i am unable to found my desired result.
  My Project Model code:</p>
  <pre><code>class Project extends Model
  {
      function progress(){
          return $this-&gt;hasMany(Progress::class);
      }
  }
  </code></pre>
  <p>Progress Model Code:</p>
  <pre><code>class Progress extends Model
  {
      function project(){
          return $this-&gt;belongsTo(Project::class);
      }
  
      function task(){
          return $this-&gt;hasMany(Task::class);
      }
  }
  </code></pre>
  <p>Task Model Code:</p>
  <pre><code>class Task extends Model
  {
  
      protected $fillable = [
          'progress',
      ];
  
      function progress(){
          return $this-&gt;belongsTo(Progress::class, 'id');
      }
  }
  </code></pre>
  <p>Here is my Controller Function:</p>
  <pre><code>public function task_project_view_detail($id){
          $project = Project::with(['user', 'image', 'document', 'progress.task'])-&gt;where('id', $id)-&gt;first();
          $progress = $project-&gt;progress;
          return view('admin.tasks.project-view', ['progress' =&gt; $progress])-&gt;with('project', $project);
      }
  </code></pre>
  <p>and Here is my View File:</p>
  <pre><code>@foreach ($progress as $prog)
    @foreach ($prog-&gt;task as $tasks)
      &lt;li class=&quot;task&quot;&gt;
        &lt;div class=&quot;task-container&quot;&gt;
          &lt;span class=&quot;task-action-btn task-check&quot;&gt;
            &lt;a href=&quot;&quot; style=&quot;background:#3d981f;&quot; class=&quot;action-circle large complete-btn&quot; title=&quot;Mark Complete&quot;&gt;
              &lt;i class=&quot;material-icons&quot;&gt;check&lt;/i&gt;
            &lt;/a&gt;
          &lt;/span&gt;
          &lt;span class=&quot;task-label&quot; contenteditable=&quot;true&quot;&gt;{{ $tasks-&gt;name }}&lt;/span&gt;
          &lt;span class=&quot;task-action-btn task-btn-right&quot;&gt;
            &lt;a href=&quot;&quot; class=&quot;action-circle large&quot; title=&quot;Delete Task&quot;&gt;
              &lt;i class=&quot;material-icons&quot;&gt;delete&lt;/i&gt;
            &lt;/a&gt;
          &lt;/span&gt;
        &lt;/div&gt;
      &lt;/li&gt;
    @endforeach
  @endforeach
  </code></pre>
  <p>and Here is my result:
  <a href="https://i.stack.imgur.com/nYQYk.png" rel="nofollow noreferrer">there should be only first 3 rows, but all tasks in this project are showing.</a></p>
  <p><a href="https://i.stack.imgur.com/LhmAe.png" rel="nofollow noreferrer">There should be only 1 row, the 4th one.</a></p>
  <p>Here is Schema of Project Model:</p>
  <pre><code>Schema::create('projects', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;unsignedInteger('user_id')-&gt;nullable();
              $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade')-&gt;onUpdate('cascade');
              $table-&gt;string('title');
              $table-&gt;timestamps();
  });
  </code></pre>
  <p>Here is Schema of Progress Model:</p>
  <pre><code>Schema::create('progress', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;unsignedInteger('project_id')-&gt;nullable();
              $table-&gt;foreign('project_id')-&gt;references('id')-&gt;on('projects')-&gt;onDelete('cascade')-&gt;onUpdate('cascade');
              $table-&gt;string('name');
              $table-&gt;timestamps();
  });
  </code></pre>
  <p>and here is Schema of Task Model:</p>
  <pre><code>Schema::create('tasks', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;unsignedInteger('progress_id')-&gt;nullable();
              $table-&gt;foreign('progress_id')-&gt;references('id')-&gt;on('progress')-&gt;onDelete('cascade')-&gt;onUpdate('cascade');
              $table-&gt;string('name')-&gt;nullable();
              $table-&gt;string('status')-&gt;nullable();
              $table-&gt;timestamps();
  });
  </code></pre>
  <p>What should I do?</p>