title: Laravel get relation count where a field in the database is set to a certain value
body: <p>I have a Task model and a Step model. Each Task can have multiple steps. I want to be able to display all tasks including a progress bar. The length of the progress bar will be calculated by the total steps and the total steps that are completed. So I will do a calculation like:</p>
  
  <pre><code>100% / $total_steps * $total_steps_completed
  </code></pre>
  
  <p>A step that has been completed has it's status in the database set to 1. I know how to get the $total_steps. I just do a count($task->steps) so it counts the total rows in the array.</p>
  
  <p>But how can I count the steps that have a status of 1?</p>
  
  <p>Task Model:</p>
  
  <pre><code>class Task
  {
      public function steps()
      {
          return $this-&gt;hasMany('App\Step');
      }
  }
  </code></pre>
  
  <p>Step Model:</p>
  
  <pre><code>class Step
  {
      public function task()
      {
          return $this-&gt;belongsTo('App\Task');
      }
  }
  </code></pre>
  
  <p>TasksController:</p>
  
  <pre><code>class TasksController
  {
      public function index()
      {
          $tasks = User::find(Auth::user()-&gt;id)-&gt;tasks-&gt;with('steps')-&gt;get();
  
          return view('tasks', compact('tasks');
      }
  }
  </code></pre>
  
  <p>Each Task belongs to a user, hence the <code>User::find()-&gt;tasks</code> etc..</p>
  
  <p>In my <code>steps</code> table I have the following field:</p>
  
  <pre><code>$table-&gt;integer('status');
  </code></pre>
  
  <p>This can be set to either 0 or 1. In my Tasks -> <code>index.blade.php</code> view I have a simple foreach:</p>
  
  <pre><code>@foreach($tasks as $task)
      {!! $task-&gt;id !!} // This is 1 for example
  
      Total Steps: {!! count($task-&gt;steps) !!} // Returns 5 if there are 5 rows in the steps database with task_id `1` for example
  
  @endforeach
  </code></pre>
  
  <p>Now, what I need, is to count the total steps that have it's status set to <code>1</code>, so I can perform my calculation and display the progress bar. I think I should add a function for this, or maybe a scope, in my steps model. But I am stuck with this, so any pointers will be appreciated.</p>