I have models with appropriate belongsTo/hasMany relationships defined, and I want to query one table based on properties of a joined table. I can do it if I define the join in my query, but is there something I can do so that I don't have to define the join everytime I want to query a model on it's relation?
Forinstance. the following does not work, unless I define a join:
FileAssociation::with('file')->where('files.file_type_version_id', $file->file_type_version_id)->get()
I think what you want is Eager Load Constraints basically you can do this
FileAssociation::with(array('file' => function($query)
{
$query->where('file_type_version_id',$file->file_type_version_id);
}))->get();
Check out the docs on Eager Loading
Edit
If you are using Laravel 4.1 now you have access to the whereHas()
method if you want to return the FileAssociation
only if there is at least one "file"
FileAssociation::whereHas('file' => function($query)
{
$query->where('file_type_version_id',$file->file_type_version_id);
})->get();
Check out the docs on Quering Relations