Using BaseTrait instead of BaseModel?
Actually I have a BaseModel which cares about storing created_by stuff and some getters and setters for usernames, checkbox values etc.
For several reasons I want to change that code to one or more Traits.
I try to create one BaseTrait which cares for the public static function boot() and a ModelAttributesTrait which cares for the getters and setters of attributes.
Everytime when I shift all the setter and getters to ModelAttributesTrait, I'll get a problem to Login into the system. The error is that my user has been deleted, which in fact is not true. I'l use toddish/verify package for Auth.
So, here is my original Base Model:
<?php
use Carbon\Carbon;
class BaseModel extends Eloquent {
// Select the dateformat to show in all views
protected $dateformat = 'adminpanel.dateformat.date_short_time_short';
// Functions to use while booting this class
public static function boot()
{
parent::boot();
// Save values for created_by and updated_by while creating Object
static::creating(function($model)
{
if (Auth::check())
{
$model->created_by = Auth::user()->id;
$model->updated_by = Auth::user()->id;
}
else
{
$model->created_by = 0;
$model->updated_by = 0;
}
});
// Save value for updated_by while updating Object
static::updating(function($model)
{
if (Auth::check())
{
$model->updated_by = Auth::user()->id;
}
else
{
$model->updated_by = 0;
}
});
// Save value for deleted_by while soft-deleting Object
static::deleting(function($model)
{
if (Auth::check())
{
$model->deleted_by = Auth::user()->id;
$model->save();
}
else
{
$model->updated_by = 0;
$model->save();
}
});
}
// Show readable created_at value
public function getCreatedAtAttribute($attr)
{
return Carbon::parse($attr)->formatLocalized(''.trans($this->dateformat).'');
}
// Show readable updated_at value
public function getUpdatedAtAttribute($attr)
{
return Carbon::parse($attr)->formatLocalized(''.trans($this->dateformat).'');
}
// Show readable deleted_at value
public function getDeletedAtAttribute($attr)
{
return Carbon::parse($attr)->formatLocalized(''.trans($this->dateformat).'');
}
public function getLastLoginAttribute($attr)
{
if ($attr != '0000-00-00 00:00:00')
{
return Carbon::parse($attr)->formatLocalized(''.trans($this->dateformat).'');
}
else
{
return '';
}
}
// Show readable created_by value
public function getCreatedByAttribute($attr)
{
if ($attr)
{
return User::findOrFail($attr)->username;
}
else
{
return 'SYSTEM';
}
}
// Show readable updated_by value
public function getUpdatedByAttribute($attr)
{
if ($attr)
{
return User::findOrFail($attr)->username;
}
else
{
return 'SYSTEM';
}
}
// Show readable deleted_by value
public function getDeletedByAttribute($attr)
{
if ($attr)
{
return User::findOrFail($attr)->username;
}
else
{
return 'SYSTEM';
}
}
// Show readable active value
public static function getActiveAttribute($attr)
{
if ($attr)
{
return trans('adminpanel.activestates.'.$attr);
}
else
{
return trans('adminpanel.activestates.0');
}
}
}
My new BaseTrait:
<?php namespace Acme\Traits;
use Carbon\Carbon;
trait BaseTrait {
// Select the dateformat to show in all views
protected $dateformat = 'adminpanel.dateformat.date_short_time_short';
// Functions to use while booting this class
public static function boot()
{
parent::boot();
// Save values for created_by and updated_by while creating Object
static::creating(function($model)
{
if (\Auth::check())
{
$model->created_by = \Auth::user()->id;
$model->updated_by = \Auth::user()->id;
}
else
{
$model->created_by = 0;
$model->updated_by = 0;
}
});
// Save value for updated_by while updating Object
static::updating(function($model)
{
if (\Auth::check())
{
$model->updated_by = \Auth::user()->id;
}
else
{
$model->updated_by = 0;
}
});
// Save value for deleted_by while soft-deleting Object
static::deleting(function($model)
{
if (\Auth::check())
{
$model->deleted_by = \Auth::user()->id;
$model->save();
}
else
{
$model->updated_by = 0;
$model->save();
}
});
}
}
And finally, the try to my ModelAttributesTrait, which seems to cause this problem:
<?php namespace Acme\Traits;
trait ModelAttributesTrait {
/**
* Get the actionfields attributes for the model.
*
* @return array
*/
public function getActionfields()
{
return $this->actionfields;
}
/**
* Get the properties attributes for the model.
*
* @return array
*/
public function getProperties()
{
return $this->properties;
}
// Show readable created_at value
public function getCreatedAtAttribute($attr)
{
return Carbon::parse($attr)->formatLocalized(''.trans($this->dateformat).'');
}
// Show readable updated_at value
public function getUpdatedAtAttribute($attr)
{
return Carbon::parse($attr)->formatLocalized(''.trans($this->dateformat).'');
}
// Show readable deleted_at value
public function getDeletedAtAttribute($attr)
{
return Carbon::parse($attr)->formatLocalized(''.trans($this->dateformat).'');
}
public function getLastLoginAttribute($attr)
{
if ($attr != '0000-00-00 00:00:00')
{
return Carbon::parse($attr)->formatLocalized(''.trans($this->dateformat).'');
}
else
{
return '';
}
}
// Show readable created_by value
public function getCreatedByAttribute($attr)
{
if ($attr)
{
return \User::findOrFail($attr)->username;
}
else
{
return 'SYSTEM';
}
}
// Show readable updated_by value
public function getUpdatedByAttribute($attr)
{
if ($attr)
{
return \User::findOrFail($attr)->username;
}
else
{
return 'SYSTEM';
}
}
// Show readable deleted_by value
public function getDeletedByAttribute($attr)
{
if ($attr)
{
return \User::findOrFail($attr)->username;
}
else
{
return 'SYSTEM';
}
}
// Show readable active value
public static function getActiveAttribute($attr)
{
if ($attr)
{
return trans('adminpanel.activestates.'.$attr);
}
else
{
return trans('adminpanel.activestates.0');
}
}
}
I hope someone can give me a hint to understand what is happening here. Thanks Andreas
Please or to participate in this conversation.