Created
November 6, 2016 20:22
-
-
Save alexweissman/2220b263f376a107b07d42e90ce6ba8a to your computer and use it in GitHub Desktop.
Represents a Worker and various helper scope queries
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 UserFrosting; | |
| use Illuminate\Database\Capsule\Manager as Capsule; | |
| use Illuminate\Database\Eloquent\SoftDeletes; | |
| /** | |
| * Worker Class | |
| * | |
| * Represents a worker, which is a special type of user. | |
| * | |
| * @author Alex Weissman | |
| * | |
| * @property string name | |
| */ | |
| class Worker extends UFModel | |
| { | |
| /** | |
| * @var string The id of the table for the current model. | |
| */ | |
| protected static $_table_id = "worker"; | |
| public $timestamps = true; | |
| /** | |
| * Use soft deletes, so we can maintain related entities (tasks, etc). | |
| */ | |
| use SoftDeletes; | |
| /** | |
| * Get the user account associated with this worker. | |
| */ | |
| public function user() | |
| { | |
| return $this->belongsTo('UserFrosting\User', 'user_id'); | |
| } | |
| /** | |
| * Get all tasks for this worker. | |
| */ | |
| public function tasks() | |
| { | |
| return $this->hasMany('UserFrosting\Task', 'worker_id'); | |
| } | |
| /** | |
| * Get all paychecks for this worker. | |
| */ | |
| public function paychecks() | |
| { | |
| return $this->hasMany('UserFrosting\Paycheck', 'worker_id'); | |
| } | |
| /** | |
| * Joins the worker's user record directly, so we can do things like sort, search, paginate, etc. | |
| */ | |
| public function scopeJoinUser($query) | |
| { | |
| return $query->select('worker.*', 'user.user_name as user_name')->join('user', 'worker.user_id', '=', 'user.id'); | |
| } | |
| /** | |
| * Load basic user info for this worker. | |
| */ | |
| public function scopeWithUserStub($query) | |
| { | |
| return $query->with(['user' => function ($subQuery) { | |
| return $subQuery->select('id', 'user_name', 'display_name', 'title'); | |
| }]); | |
| } | |
| /** | |
| * Joins the worker's most recent task directly, so we can do things like sort, search, paginate, etc. | |
| */ | |
| public function scopeJoinLatestTask($query, $taskFilter = null) | |
| { | |
| $query = $query->select('worker.*', 'task.completed_at as last_task_date'); | |
| if ($taskFilter) { | |
| $filterClause = Capsule::raw(" | |
| (select id from task where | |
| task.worker_id = worker.id | |
| and task.description like ? | |
| order by completed_at desc | |
| limit 1)"); | |
| return $query->leftJoin('task', 'task.id', '=', $filterClause)->setBindings(["%$taskFilter%"]); | |
| } else { | |
| $filterClause = Capsule::raw(" | |
| (select id from task where | |
| task.worker_id = worker.id | |
| order by completed_at desc | |
| limit 1)"); | |
| return $query->leftJoin('task', 'task.id', '=', $filterClause); | |
| } | |
| } | |
| /** | |
| * Get the most recent task for this worker. | |
| */ | |
| public function latestTask() | |
| { | |
| return $this->hasOne('UserFrosting\Task', 'worker_id')->latest('completed_at'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment