2017 © Pedro Peláez
 

library eloquent-otf

image

calhoun/eloquent-otf

  • Monday, July 31, 2017
  • by mauricecalhoun
  • Repository
  • 3 Watchers
  • 70 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Eloquent On The Fly

Eloquent On The Fly is a package that has been designed to help you to use Laravel's Eloquent to query array., (*1)

Installation

To install through composer, simply put the following in your composer.json file:, (*2)

{
    "require": {
        "calhoun/eloquent-otf": "*"
    }
}

And then run composer install from the terminal., (*3)

Quick Installation

Above installation can also be simplify by using the following command:, (*4)

composer require "calhoun/eloquent-otf"

Next, if you are using a version of Laravel less than 5.5 you should add the OTFServiceProvider to the providers array of your config/app.php configuration file:, (*5)

Calhoun\OTF\OTFServiceProvider::class,    

Usage

Use as a Helper

To use Eloquent On The Fly as a Helper, all you need is an associative array, and closure containing your eloquent or builder query., (*6)

<?php

$data = [
  [
    'id'    => 1
    'first' => 'Maurice',
    'last'  => 'Calhoun',
    'email' => 'maurice@mauricecalhoun.com',
    'age'   => '40',
    'manager_id' => 2
  ],
  [
    'id'    => 2
    'first' => 'John',
    'last'  => 'Doe',
    'email' => 'manager@job.com',
    'age'   => '45',
    'manager_id' => null
  ],
  ...
];

$result = eloquent($data, function($query){
    return $query->whereAge(40)->get();
});

// You can pass a third parameter for the table name (by default the name is oft)
 $result = eloquent($data, function($query, $name){
   return $query->join($name . " as manager", $name.'.manager_id', '=', 'manager.id')->find(1);
 }, 'employees');

Use as a Collection Method

To use Eloquent On The Fly as a Collection Method, all you need is a closure containing your eloquent or builder query., (*7)

<?php

$data = [
  [
    'first' => 'Maurice',
    'last'  => 'Calhoun',
    'email' => 'maurice@mauricecalhoun.com',
    'age'   => '40'
  ],
  ...
];

$result = collect($data)->filter(function($item){
    return item['age'] >= 40;
})->eloquent(function($query){
  return $query->where('email', 'like', '%mauricecalhoun.com')->get();
});

Use as a Class

Simple

To use Eloquent On The Fly as a Class. Instantiate the OTF Class and use the create method, which takes a name and data as its parameters., (*8)

<?php

use  Calhoun\OTF\Support\OTF;

$data = [
  [
    'first' => 'Maurice',
    'last'  => 'Calhoun',
    'email' => 'maurice@mauricecalhoun.com',
    'age'   => '40'
  ],
  ...
];

$otf = app()->make(OTF::class)->create('person', $data);
$maurice = $otf->person->whereLast('Calhoun')->first();
Advance

You can use Eloquent On The Fly as a Class with relationships., (*9)

```php <?php, (*10)

use Calhoun\OTF\Support\OTF;, (*11)

$users = [ [ 'id' => 1 'first' => 'Maurice', 'last' => 'Calhoun', ], ... ];, (*12)

$profiles = [ [ 'id' => 1 'email' => 'maurice@mauricecalhoun.com', 'age' => '40', 'user_id' => 1 ], ... ];, (*13)

$oft = app()->make(OTF::class);, (*14)

$user = $otf->create('user', $users); $profile = $otf->create('profile',$profiles);, (*15)

$relationships = [ 'user' => [ 'profile' => function($self) use($profile){ return $self->hasOne($profile, 'user_id'); } ], 'profile' => [ 'user' => function($self) use($user){ return $self->belongsTo($user, 'id'); } ] ];, (*16)

$otf->setRelationships($relationships);, (*17)

$maurice = $user->find(1)->profile;, (*18)

The Versions