Skip to content

Instantly share code, notes, and snippets.

@emtudo
Forked from vluzrmos/Post.php
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emtudo/1f4ddf97fa6b7a6bf794 to your computer and use it in GitHub Desktop.
Save emtudo/1f4ddf97fa6b7a6bf794 to your computer and use it in GitHub Desktop.
Example how to remove the eager loading on Eloquent.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Array of relationships to load.
* @var array
*/
protected $with = ['comments'];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
/**
* @param array $relations
* @return static
*/
public static function onlyWith($relations = [])
{
$instance = new static;
$instance->with = $relations;
return $instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment