-
app/Traits/DeadlineTrait.php
Open in GitHub// Trait is a re-usable structure that can be added to any PHP class // In this case, DeadlineTrait will be added to three Models that can have deadline-related methods // Notice: there's no command like "php artisan make:trait", you need to create this file manually trait DeadlineTrait { public function isOverDeadline():bool { if ($this->isClosed()) { return false; } return $this->deadline < Carbon::now(); } public function isCloseToDeadline(Int $days = 2):bool { return $this->deadline < Carbon::now()->addDays($days); } public function getDaysUntilDeadlineAttribute(): Int { return Carbon::now()->startOfDay()->diffInDays($this->deadline, false); } }
-
app/Models/Project.php
Open in GitHubuse App\Traits\DeadlineTrait; class Project extends model implements Commentable { use SoftDeletes, SearchableTrait, DeadlineTrait; // ... other model properties/methods }
-
app/Models/Task.php
Open in GitHubuse App\Traits\DeadlineTrait; class Task extends Model implements Commentable { use SearchableTrait, SoftDeletes, DeadlineTrait; // ... other model properties/methods }
-
app/Models/Lead.php
Open in GitHubuse App\Traits\DeadlineTrait; class Lead extends Model implements Commentable { use SearchableTrait, SoftDeletes, DeadlineTrait; // ... other model properties/methods }
-
resources/views/projects/_sidebar.blade.php
Open in GitHub// Trait methods can be used, for example, in Blade, as if they are regular Model methods // Look at isCloseToDeadline() here <div class="row margin-top-10"> <div class="col-md-3">{{ __('Deadline') }}</div> <div class="col-md-9"> <span class="siderbar-list-value {{$project->isCloseToDeadline() ? 'text-danger' : ''}}"> {{date(carbonDate(), strTotime($project->deadline))}} @if($project->isCloseToDeadline()) <span class="small text-black">({!! $project->days_until_deadline !!})</span> @endif // ... other sidebar code