Skip to content

Instantly share code, notes, and snippets.

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 whoisryosuke/49f4e69632973069d6f397ad0226ea0a to your computer and use it in GitHub Desktop.
Save whoisryosuke/49f4e69632973069d6f397ad0226ea0a to your computer and use it in GitHub Desktop.
Laravel - Automatically eager load model relationships by setting a protected $with var on your model -- found via: https://stackoverflow.com/a/25674216
<?php
namespace YourApp;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
/**
* Automagically eager load any Model relationships
*
* @var array
*/
protected $with = ['categories'];
/**
* Get the category relationship
*/
public function categories()
{
return $this->hasMany('YourApp\Categories', 'category_id', 'id');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment