Giter VIP home page Giter VIP logo

laravel-precedent's Introduction

laravel-precedent

Auto Cache and Purge on Eloquent

Features

  1. Auto cache on find method and purge on update, delete.
  2. Validate via model.
  3. Possible to sync data for fields in the pivote table.

Installation

Install this bundle by running the following CLI command:

php artisan bundle:install precedent

Add the following line to application/bundles.php

'precedent' => array(
	'autoloads' => array(
		'map' => array(
			'Has_Many_And_Belongs_To' => '(:bundle)/has_many_and_belongs_to.php',
			'Precedent'               => '(:bundle)/model.php'
		),
	)
),

Example Usage

Enable config profile to debug.

'profiler' => true

Enable cache using attribute $cache in model.

class User extends Precedent {

	public static $cache = true;

}

Auto Cache on find method (Work only static method).

// Cache on find.
// This works.
User::find(10);

// This doesn't work.
$user = new User;
$user->find(10)

Auto Purge on delete and update method.

$user = User::find(10);

// Purge cache on delete.
$user->delete();

// and

// Purge cache on update.
$user = User::find(10);
$user->fill(array(
	'name' => 'Tee'
));

$user->save();

Bonus 1

Validate via model

class User extends Precedent {

	/**
	 * Cache enabled
	 *
	 * @type {Boolean}
	 */
	public static $cache = true;

	/**
	 * Validate rules
	 *
	 * @type array
	 */
	public static $rules = array(
		'email' => 'required|unique:users'
	);

	/**
	 * Validate messages
	 *
	 * @type array
	 */
	public static $messages = array(
		'email_required' => 'Please enter you email.',
		'email_email'    => 'Please enter valid email.'
	);

}

Your controller.

$user = new User; // or User::find(1);

$data = array(
	'email' => Input::get('email')
);

if ( ! $user->valid($data))
{
	$errors = $user->errors;

	return Redirect::back()->with_errors($errors)->with_input();
}

Bonus 2

Can add attributes at method 'sync' for has_many_and_belongs_to relationship.

$user = User::find(1);
$user->roles()->sync(array(1, 3, 9), array('remark' => 'wow'));

Support or Contact

If you have some problem, Contact [email protected]

laravel-precedent's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.