Skip to content

Instantly share code, notes, and snippets.

@purwandi
Created June 15, 2012 02:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save purwandi/2934321 to your computer and use it in GitHub Desktop.
Save purwandi/2934321 to your computer and use it in GitHub Desktop.
My Base Model for Laravel
<?php
/**
* Base Model
*
* @package Extends Model Laravel
* @version 1.0
* @author Purwandi <free6300@gmail.com>
* @link http://purwand.me
*/
class Base extends Eloquent{
/**
* Property field on table
*
* Example
*
* Class Foo extends Base {
* protected $rules = array(
* 'color' => 'required|alpha|min:3',
* 'size' => 'required',
* // .. more rules
* );
* }
* @var array
*/
protected $property = array();
protected $errors;
/**
* Validate model property
*
* @param array $ignore if you want to ignore key
* @return bool
*/
public function validate($ignore = array())
{
// set ignore field
foreach ($ignore as $key)
{
if (in_array($key,$this->property))
{
// unset ignore property
unset($this->property[$key]);
}
}
$failed = Validator::make(Input::all(), $this->property);
if ($failed->fails())
{
$this->errors = $failed->errors;
return false;
}
return true;
}
/**
* Catch error property
*
* @return array
*/
public function errors()
{
return $this->errors;
}
/**
* Save method
*
* You must create instance first
*
* @param object $instance [description]
* @return void
*/
public function save($instance)
{
foreach (Input::get('all') as $key => $value)
{
if (in_array($key,$this->property))
{
$instance->{$key} = $value;
}
}
return $instance->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment