al-one/eloquent-super-relations

Laravel Eloquent Super Relations

v1.6.20-beta1 2021-05-18 03:00 UTC

This package is auto-updated.

Last update: 2024-10-30 01:37:19 UTC


README

Installing

# composer.json

"minimum-stability": "dev",
"prefer-stable": true,
$ composer require al-one/eloquent-super-relations -vvv

Usage

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Alone\EloquentSuperRelations\HasSuperRelations;

class User extends Model
{

    use HasSuperRelations;

    public function profile()
    {
        return $this->hasOne('App\Profile', 'uid');
    }

    /**
     * @return  Model|Collection|array|null
     */
    public function eagerLoadProfile($relation, $models = [], $where = [])
    {
        // Get cached data for relation
        if(!empty($where['uid'])) {
            return cache()->remember("user:profile:{$where['uid']}", 86400, function() use($where) {
                return Profile::find($where['uid']);
            });
        }
        // return null for get from database
        return null;
    }

}

License

MIT