luizgabriel/infire

An extension for eloquent class (Laravel)

This package's canonical repository appears to be gone and the package has been frozen as a result.

dev-master 2015-02-26 20:55 UTC

This package is not auto-updated.

Last update: 2019-03-09 00:58:33 UTC


README

An extension for the Eloquent class (Laravel)

##Contents

  • [Installation] (#instalation)
  • [Getting Started] (#getting-started)
  • [Validation with Infire] (#validation-with-infire)
  • [Auto Input Model Filling] (#auto-input-model-filling)
  • [Activing or Deactiving Infire Properties] (#activing-or-deactiving-infire-properties)
  • [File Uploading] (#file-uploading)
  • [Using the Uploader Directly] (#using-the-uploader-directly)

##Installation

Add luizgabriel/infire as a requirement to composer.json:

  "require" : {
        "luizgabriel/infire": "1.0.*@dev"
  }

Update your packages with composer update or install with composer install.

Getting Started

To create a new Infire model, simply make your model class derive from the Infire base class. In the next examples we will use the complete namespaced class to make examples cleaner, but you're encouraged to make use of use in all your classes:

use Luizgabriel\Infire\Infire;

class MyModel extends Infire
{
   // variables and methods
}

Note: you can also add an alias to your laravel configurations by adding 'Infire' => 'Luizgabriel\Infire\Infire'

Validation with Infire

Infire models use Laravel's built-in Validator class. Defining validation rules for a model is simple and is typically done in your model class as a static variable:

class User extends Infire
{
    public static $rules = [
        'name'                  => 'required|between:4,16',
        'email'                 => 'required|email',
        'password'              => 'required|alpha_num|between:4,8|confirmed',
        'password_confirmation' => 'required|alpha_num|between:4,8',
    ];
}

Infire models validate themselves automatically when Infire->save() is called.

$user           = new User;
$user->name     = 'John doe';
$user->email    = 'john@doe.com';
$user->password = 'test';

$success = $user->save(); // returns false if model is invalid

##Auto Input Model Filling

With infire you can auto fill your model attributes from a form input. For example, let's write a single controller method code.

class MyController extends BaseController
{
    public function store()
    {
        $user = new User;
        
        if(!$user->save())
          return Redirect::back()->withErrors($user->errors());
        
        Redirect::route('users.index');
    }
}

It will have the same effect as

class MyController extends BaseController
{
    public function store()
    {
        $user           = new User;
        $user->name     = Input::get('name');
        $user->email    = Input::get('email');
        $user->password =  Hash::make(Input::get('password'));
        //Other fields...
        
        if(!$user->save())
          return Redirect::back()->withErrors($user->errors());
        
        Redirect::route('users.index');
    }
}

##Activing or Deactiving Infire Properties You can deactivate the Auto Fill Model by having a public $autoFillAttributes = false;. Infire auto hash your password fields too, you can deactivate this option by having public $autoHashPassword = false;.

##File Uploading Infire can handle all upload stuff by only having a single array of files like

class User extends Infire
{
    protected $fillable = [
        'thumb',
        'profile_image',
        'name',
        'email',
        'password',
    ];

    public $files = ['thumb', 'profile_image'];
    /*
     * When $user->save() is called.
     * Your file input is catch and sent to your Cloudinary storage acount.
     */
}

##Using the Uploader Directly When you have some special kind of upload which is not suported by the default post-upload, you can use the $model->uploader(). This gives you the current uploader to use (based on your configurations). For example:

use Symfony\Component\HttpFoundation\File\UploadedFile as File;

class Photo extends Infire
{
    protected $fillable = [
        'image_file',
        'user_id'
    ];

    public $files = ['image_file'];
    
    public function setImageFileAttribute(File $value)
    {
        $upload = $this->uploader()->upload($file); //Sends the file to the storage server and returns informations about it
        $this->attributes['image_file'] = $this->uploader()->uploadToJson($upload); //Grabs only the important informations
    }
    
    public function getImageFileAttribute($value, $options = array())
    {
        /*
         * Here you can check if there is no image and return a default one.
         * It's up to your imagination.
         */
        return $this->uploader()->toUrl($value, $options);
    }
  
}

For the Cloudinary Uploader, the $options array can be used to set informations like width, height, crop and others. (Check more at the [Cloudinary Documentation] (http://cloudinary.com/documentation/image_transformations))