Q:

Laravel: Fetch with where filter on "with"

In Laravel you can fetch a related database entry in one query with the with method like this:
$customer = Customer::with('user')->where([
    'id' =>'1'
])->first();

Which will return a customer and it's related user object. Is it possible to apply a filter to this with item? I thought that this would be possible:
$customer = Customer::with('user')->where([
    'id' =>'1',
    'users.status' => 'activated'
])->first();

But that does not seem to work, it tries to find a column "users.status" instead of a column status in the users table. 

A:

You can use Eager loading constraints. It allows you to perform queries on related models.
Like this:
$customer = Customer::with(['user' => function($query){
    $query->where('status', 'activated'); //in the user table
}])->where('id', '1')->first(); //in the customer table

You can read the eager load constaints part for more informations:
http://laravel.com/docs/4.2/eloquent#eager-loading

