Skip to content

Instantly share code, notes, and snippets.

@mazyvan
Last active December 7, 2018 18:34
Show Gist options
  • Save mazyvan/4ab7bab380bdd10108d4027f8630f9bd to your computer and use it in GitHub Desktop.
Save mazyvan/4ab7bab380bdd10108d4027f8630f9bd to your computer and use it in GitHub Desktop.
A simple templete for your Laravel's Eloquent Model
<?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