Last active
January 30, 2018 23:03
-
-
Save Snaver/4467e80f6c6d881b53240fb1283d141e to your computer and use it in GitHub Desktop.
Cleaning up & Simplifying Eloquent Models (using Traits)
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 | |
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'); | |
} | |
} |
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 | |
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(); | |
} | |
} |
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 | |
namespace App\Models\Agreement; | |
trait Accessor | |
{ | |
/** | |
* Get the Primary key | |
* | |
* @return string | |
*/ | |
public function getIdAttribute($value) | |
{ | |
return $this->AgreementId; | |
} | |
} |
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 | |
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(); | |
} | |
} |
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 | |
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'); | |
} | |
} |
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 | |
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