delatbabel / fluents by delatbabel

Convert Eloquent Model objects to and from Fluent objects.
1,346
0
3
Package Data
Maintainer Username: delatbabel
Maintainer Contact: del@babel.com.au (Del)
Package Create Date: 2015-12-06
Package Last Update: 2017-06-26
Language: PHP
License: MIT
Last Refreshed: 2024-11-11 15:07:14
Package Statistics
Total Downloads: 1,346
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 3
Total Forks: 1
Total Open Issues: 0

Fluents

Laravel extension to convert to/from Fluent objects.

Rationale

The Laravel Fluent object is one of the most under-rated and under-utilized components of the framework. Essentially a free-standing object, it allows you to access an array of attributes as an object rather than as an array.

example:

Instead of using an array like this:

$myArray = [];
$myArray['first'] = 'one';
$myArray['second'] = 'two;

echo $myArray['first']; // prints "one"

Use fluent object like this:

$myObject = new Fluent();
$myObject->first = 'one';
$myObject->second = 'two';

echo $myObject->first; // prints "one"

Until now, however, there has been no standard straightforwards way to convert an Eloquent Model object into a Fluent object. There is a toArray() function on a Model object but no equivalent toFluent() function.

This component adds such functions using a trait that can be applied to any model object. Note that several other of Laravel's internal classes can also have this trait applied, as long as they store their data in an internal attributes array this trait should work.

Usage

Model Fill From Fluent Object

use Delatbabel\Fluents\Fluents;

class User extends Eloquent {
    use Fluents;
}

$myFluent = new Fluent();
$myFluent->first = 'one';

$myUser = new User();
$myUser->fromFluent($myFluent);

Model Convert From Fluent Object

use Delatbabel\Fluents\Fluents;

class User extends Eloquent {
    use Fluents;
}

$myUser = User->find(1);
$myFluent = User->toFluent();