Skip to content

A library for implementing repositories with magic finders for the Eloquent ORM.

License

Notifications You must be signed in to change notification settings

pushoperations/Magician

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magician

Build Status Coverage Status Scrutinizer Code Quality

Total Downloads Latest Stable Version Latest Unstable Version License

SensioLabsInsight

A library for implementing repositories with magic finders and caching for the Eloquent ORM.

Contents

Install

The recommended way to install is through Composer.

Update your project's composer.json file to include Magic Repository:

{
    "require": {
        "pushoperations/magician": "2.*"
    }
}

Then update the project dependencies to include this library:

composer update pushoperations/magician

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

Usage

A base implementation of the magic repository is already created for use out-of-the-box.

<?php namespace Controllers;

use Controller;
use Magician\Magician;

class ExampleController extends Controller
{
    public function __construct(Magician $magician)
    {
        // Tell this magician instance to be the repository manager for the 'User' model.
        $this->m = $magician->set('Models\User');
    }

    public function create()
    {
        $user = $this->m->firstOrMake(['email' => 'user@example.com']);

        if ($this->m->save($user)) {
            return $user;
        } else {
            return 'error: unable to save the user';
        }
    }

    public function read($id = null)
    {
        if ($id) {
            return $this->m->findById($id);
        } else {
            return $this->m->getById(['>', 0]);
        }
    }

    public function update($id)
    {
        $user = $this->m->findById($id);
        $user->fill([
            'trial' => true,
            'last_login' => new \DateTime,
            'subscription' => '2015',
        ]);

        $user->load('permissions');

        if ($this->rm->save($user)) {
            return $user;
        } else {
            return 'error: unable to save the user';
        }
    }

    public function inactive($date)
    {
        return $this->m->getByLastLogin(['<', $date]);
    }

    public function newTrials()
    {
        return $this->m->get10ByTrial(true, ['subscription' => 'asc'], ['email', 'subscription']);
    }
}

About

A library for implementing repositories with magic finders for the Eloquent ORM.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages