Skip to main content

Command Palette

Search for a command to run...

Laravel Eager Loading By Default

Published
1 min readView as Markdown

Sometimes you might want to always load some relationships when retrieving a model. To accomplish this, you may define a $with property on the model:

class MemberPracticeHistory extends Model
{
    const TABLE_NAME = 'h247app_member_practice_histories';

    protected $table = self::TABLE_NAME;
    protected $primaryKey = 'prachis_id';

    const CREATED_AT = 'prachis_date';
    const UPDATED_AT = 'prachis_update';

    /**
     * * The relationships that should always be loaded.
     *
     * @var array
     */
    protected $with = ['practiceTest'];
}

More from this blog

Khang Nguyen

119 posts