Using Eloquent Has, With, WhereHas

With - With is used for eager loading. When you have more than one models and you want to load the relation for each of them Laravel will preload all that relationships. You have to run only one extra query.  An example of With is given below.

User > hasMany > Post

$users = User::with('posts')->get();

foreach($users as $user){

    $users->posts;

}

Has - Has filters the selected model based on the relationship. It is similar to where condition. Let's look at it with an example.

User > hasMany > Task

$users = User::has('tasks')->get();

This will return only the Users that have at least one task.

WhereHas - WhereHas is similar to Has in its function but it allows you to add more filters.

$users = User::whereHas('tasks', function($q){

    $q->where('created_at', '>=', '2015-01-01 00:00:00');

})->get();

only users that have tasks from 2015 onwards are returned.