Last active
December 7, 2018 18:34
-
-
Save mazyvan/4ab7bab380bdd10108d4027f8630f9bd to your computer and use it in GitHub Desktop.
A simple templete for your Laravel's Eloquent Model
This file contains 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 | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\SoftDeletes; // optional | |
use Mazyvan\Userstamps\Userstamps; // optional | |
class ClassName extends Model | |
{ | |
use SoftDeletes; // optional | |
use Userstamps; // optional | |
/** | |
* The table associated with the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'table_name'; | |
/** | |
* The table primary key column. | |
* | |
* @var string | |
*/ | |
protected $primaryKey = 'codigo'; // only if id is not the PK | |
/** | |
* Determines if the primary key is an auto increment value. | |
* | |
* @var boolean | |
*/ | |
public $incrementing = false; // only if the PK is not auto_incrementing | |
/** | |
* The attributes that aren't mass assignable. | |
* | |
* @var array | |
*/ | |
protected $guarded = []; // or fillable | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = []; // or guarded | |
/** | |
* The accessors to append to the model's array form. | |
* | |
* @var array | |
*/ | |
protected $appends = []; | |
/** | |
* The model validation rules. | |
* | |
* @var array | |
*/ | |
public static $rules = []; | |
/** | |
* The model validation rules for update only. | |
* | |
* @var array | |
*/ | |
public static $on_update_rules = []; // optional | |
/** | |
* The attributes that should be mutated to dates. | |
* | |
* @var array | |
*/ | |
protected $dates = ['deleted_at']; | |
/** | |
* The attributes that should be cast to native types. | |
* | |
* @var array | |
*/ | |
protected $casts = []; | |
/** | |
* The "booting" method of the model. | |
* | |
* @return void | |
*/ | |
protected static function boot() // optional | |
{ | |
parent::boot(); | |
static::creating(function ($model_name) {}); | |
static::created(function ($model_name) {}); | |
static::updating(function ($model_name) {}); | |
static::updated(function ($model_name) {}); | |
static::deleting(function ($model_name) {}); | |
static::deleted(function ($model_name) {}); | |
} | |
/** | |
* Get the model_name's field_name. | |
* | |
* @param mixed $value | |
* @return string|null // data type | |
*/ | |
public function getFieldNameAttribute($value) | |
{ | |
// your custom code like -> return $value | |
} | |
/** | |
* Set the model_name's field_name. | |
* | |
* @param mixed $value | |
* @return void | |
*/ | |
public function setFieldNameAttribute($value) | |
{ | |
$this->attributes['field_name'] = NULL; // your custom value | |
} | |
/** | |
* // A description of the relation | |
*/ | |
public function model_related_name() // or in plural | |
{ | |
/** | |
* One of the following | |
*/ | |
return $this->hasOne('App\ModelRelatedClass'); | |
return $this->hasMany('App\ModelRelatedClass'); | |
return $this->belongsTo('App\ModelRelatedClass'); | |
return $this->belongsToMany('App\ModelRelatedClass'); | |
return $this->morphMany('App\ModelRelatedClass'); | |
return $this->morphToMany('App\ModelRelatedClass'); | |
return $this->morphedByMany('App\ModelRelatedClass'); | |
return $this->morphTo(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment