Skip to content

Commit

Permalink
added level feature for status priority
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian committed Apr 9, 2019
1 parent bb1685f commit 4fca281
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/app/Models/StatusHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function add(String $name, Model $model, Model $related_model = nu
$add->model_id = $model->id;

if ($guard) {
$add->user_id = auth()->guard($guard)->user()->id;
$add->user_id = auth()->guard($guard)->user()->id ?? 0;
$add->guard = $guard;
}

Expand Down
28 changes: 24 additions & 4 deletions src/app/Traits/Statusable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ trait Statusable
public function status()
{
return $this->morphOne(StatusHistory::class, 'model')
->with(['status', 'related'])
->orderBy('created_at', 'desc');
->join('status', 'status.id', '=', 'status_history.status_id')
->with(['related'])
->orderBy('status.level', 'desc')
->orderBy('status_history.created_at', 'desc');
}

public function statusHistory()
Expand All @@ -20,12 +22,30 @@ public function statusHistory()
->orderBy('created_at', 'desc');
}

public function statusHistoryLevel()
{
return $this->morphMany(StatusHistory::class, 'model')
->with(['status'])
->orderBy('created_at', 'desc');
}

public function getStatusAttribute()
{
$status = $this->status()->first();

if ($status) {
return __($status->status->key);
return __($status->key);
}

return null;
}

public function getStatusLevelAttribute()
{
$status = $this->status()->first();

if ($status) {
return $status->level;
}

return null;
Expand All @@ -36,7 +56,7 @@ public function getShortStatusAttribute()
$status = $this->status()->first();

if ($status) {
return __($status->status->short_key);
return __($status->short_key);
}

return null;
Expand Down
1 change: 1 addition & 0 deletions src/database/create_status_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CreateStatusTable extends Migration
$table->string('name');
$table->string('key');
$table->string('short_key');
$table->integer('level')->default(0);
$table->timestamps();
});
}
Expand Down

0 comments on commit 4fca281

Please sign in to comment.