PHP code example of al-one / eloquent-super-relations

1. Go to this page and download the library: Download al-one/eloquent-super-relations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

al-one / eloquent-super-relations example snippets




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;
    }

}