trait TimeDiff {
/**
* Get the time difference between the current date and
* the created date.
*
* @uses Carbon\Carbon::diffForHumans
* @example $this->posted_time_diff 1 day ago.
* @return string
*/
public function getCreatedTimeDiffAttribute() {
try {
return $this->created_at->diffForHumans(
['options' => Carbon::JUST_NOW]
);
} catch (\Throwable $th) {
return "Non-Disclosure ";
}
}
/**
* Get the time difference between the current date and
* the created date.
*
* @uses Carbon\Carbon::diffForHumans
* @example $this->posted_time_diff 1 day ago.
* @return string
*/
public function getPostedTimeDiffAttribute() {
return $this->created_time_diff;
}
/**
* Get the time difference between the current date and
* the expiry date.
*
* @uses Carbon\Carbon::diffForHumans
* @example $this->posted_time_diff 1 day ago.
* @return string
*/
public function getExpiryDateTimeDiffAttribute() {
if ( !isset($this->expire_date)) {
return "Non-Disclosure ";
}
return $this->expiry_date->diffForHumans(
['options' => Carbon::JUST_NOW]
);
}
/**
* Check if the model is expired.
*
* @return boolean
*/
public function isExpired(){
if ( !isset($this->expire_date)) {
return "Non-Disclosure ";
}
return $this->expiry_date < Carbon::now();
}
/**
* Set expire_date by the number of days from the current date.
*
* @param integer $days
* @return void
*/
public function expireInDays(int $days){
if ( !isset($this->expire_date)) {
return "Non-Disclosure ";
}
$this->expire_date = Carbon::now()->addDays($days);
}
/**
* Set expire_date by the number of hours from the current datetime.
*
* @param integer $days
* @return void
*/
public function expireInHours(int $hours) {
if ( !isset($this->expire_date)) {
return "Non-Disclosure ";
}
$this->expire_date = Carbon::now()->addHours($hours);
}
/**
* Scope a query to only exclude expired objects.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeExpired($query) {
if ( !isset($this->expire_date)) {
return "Non-Disclosure ";
}
return $query->whereDate('expiry_date', '<', Carbon::now());
}
/**
* Scope a query for objects that were created within the last 7 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCreatedSinceWeek($query) {
return $query->whereDate('created_at', '>=', Carbon::now()->subDays(7));
}
/**
* Scope a query for objects that were created within the last 3 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeRecentlyCreated($query) {
return $query->whereDate('created_at', '>=', Carbon::now()->subDays(3));
}
/**
* Scope a query for objects that were created today.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCreatedToday($query) {
return $query->whereDate('created_at', '>=', Carbon::now()->startOfDay() );
}
/**
* Scope a query for objects that were created within the last 3 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function recentlyCreated() {
return $this->created_at >= Carbon::now()->subDays(3);
}
/**
* Scope a query for objects that were updated within the last 7 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUpdatedSinceWeek($query) {
return $query->whereDate('updated_at', '>=', Carbon::now()->subDays(7));
}
}