Hi, I was wondering if is it possible to check if a relationship is loaded.
I'm using eager loading and caché so when I'm at a model instance I want to :
-
check if the relationship is loaded,
-
if it's not, then I will try to take it from caché,
- if not I fetch it using
getRelated
.
*1 My problem here is that when I try to check if it's null, it automagically loads the relationship, avoiding the caché layer. So, summing up in pseudocode:
public function getMyRelationship()
{
$cache_key = '....';
$ttl = 10 * 60;
if($this->myRelationsip === null) { // check if the related models are already fetched // *1
// get from cache or execute function to fetch the related models
$this->myRelationship = $this->cache->remember($cache_key, $ttl, function() {
return $this->getRelated('myRelationship');
}
}
return $this->myRelationship;
}