Skip to content

Instantly share code, notes, and snippets.

@YOzaz
Created November 25, 2015 15:18
Show Gist options
  • Save YOzaz/1e682f5cdaeeb7d3861d to your computer and use it in GitHub Desktop.
Save YOzaz/1e682f5cdaeeb7d3861d to your computer and use it in GitHub Desktop.
<?php
/**
* Lazy loads relationship if required
*
* @param \Eloquent $model
* @param string $relationship
*
* @return object
*/
function load( &$model, $relationship )
{
if ( !isset( $model->{$relationship} ) )
{
$model->load( $relationship );
}
return $model->{$relationship};
}
// usage example
$comments = load( $post, 'comments' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment