Created
June 15, 2012 02:14
-
-
Save purwandi/2934321 to your computer and use it in GitHub Desktop.
My Base Model for Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Base Model | |
* | |
* @package Extends Model Laravel | |
* @version 1.0 | |
* @author Purwandi <[email protected]> | |
* @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