Skip to content

Instantly share code, notes, and snippets.

@Snaver
Last active January 30, 2018 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Snaver/4467e80f6c6d881b53240fb1283d141e to your computer and use it in GitHub Desktop.
Save Snaver/4467e80f6c6d881b53240fb1283d141e to your computer and use it in GitHub Desktop.
Cleaning up & Simplifying Eloquent Models (using Traits)
<?php
namespace App\Models;
class Agreement extends BaseModel
{
const CREATED_AT = 'CreatedOn';
const UPDATED_AT = 'LastUpdateOn';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'Agreement';
/**
* The primary key associated with the model.
*
* @var string
*/
protected $primaryKey = 'AgreementId';
/**
* The relations to eager load on every query.
*
* @var array
*/
protected $with = ['business'];
/**
* Get the Primary key
*
* @return string
*/
public function getIdAttribute($value)
{
return $this->AgreementId;
}
/**
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActive($query)
{
return $query->whereNull('InactiveSince');
}
/**
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeInactive($query)
{
return $query->whereNotNull('InactiveSince');
}
public function activate()
{
$this->InactiveSince = null;
return $this->save();
}
public function deactivate()
{
$this->InactiveSince = now();
return $this->save();
}
/**
* Set the agreement number.
*
* @param string $value
* @return void
*/
public function setNumberAttribute($value)
{
$this->attributes['number'] = strtolower($value).rand();
}
/**
*
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
*/
public function business()
{
return $this->belongsTo(Business::class, 'BusinessId', 'BusinessId');
}
}
<?php
namespace App\Models;
use App\Models\Agreement\Accessors;
use App\Models\Agreement\Mutators;
use App\Models\Agreement\Relations;
use App\Models\Agreement\Scopes;
class Agreement extends BaseModel
{
use Accessors, Mutators, Relations, Scopes;
const CREATED_AT = 'CreatedOn';
const UPDATED_AT = 'LastUpdateOn';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'Agreement';
/**
* The primary key associated with the model.
*
* @var string
*/
protected $primaryKey = 'AgreementId';
/**
* The relations to eager load on every query.
*
* @var array
*/
protected $with = ['business'];
public function activate()
{
$this->InactiveSince = null;
return $this->save();
}
public function deactivate()
{
$this->InactiveSince = now();
return $this->save();
}
}
<?php
namespace App\Models\Agreement;
trait Accessor
{
/**
* Get the Primary key
*
* @return string
*/
public function getIdAttribute($value)
{
return $this->AgreementId;
}
}
<?php
namespace App\Models\Agreement;
trait Mutators
{
/**
* Set the agreement number.
*
* @param string $value
* @return void
*/
public function setNumberAttribute($value)
{
$this->attributes['number'] = strtolower($value).rand();
}
}
<?php
namespace App\Models\Agreement;
use App\Models\Business;
trait Relations
{
/**
*
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
*/
public function business()
{
return $this->belongsTo(Business::class, 'BusinessId', 'BusinessId');
}
}
<?php
namespace App\Models\Agreement;
trait Scopes
{
/**
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActive($query)
{
return $query->whereNull('InactiveSince');
}
/**
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeInactive($query)
{
return $query->whereNotNull('InactiveSince');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment