Created
April 28, 2018 00:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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