vendredi 31 juillet 2015

Laravel use eloquent to fetch the first value on a column name

Hi I'm using Laravel and to build a task board. I have several models Project, Task, Status,AssignedStatus,TaskProgress. A basic table structure is below:

Project (table name: projects)

id|name|created_at|updated_at

Task (table name: tasks)

id|name|effort|created_at|updated_at

TaskProgress (table name: task_progresses)

id|project_id|task_id|assigned_status_id|created_at|updated_at

Status (order is assigned in this table as a user can choose a set of global or assign there own to a project)

id|name|order|created_at|updated_at

AssignedStatus (table name: assigned_stauses)

id|project_id|status_id|order|created_at|updated_at

Now to build the task board I need to know what the assigned statuses are for the project in question and then grab all of the task_progresses but I only want to grab the latest task_id on the task board so I can see where it is in the cycle. As it could have moved from open to in progress to review to open again as there may be something wrong with the task. i.e. I want to fetch the first task_id but I still want to be able to retrieve all of the tasks within a given status.

To do this I am doing the following:

AssignedStatus::where('project_id','1')->with(['status','taskprogress'])->orderBy('order','asc')->get();

This is my AssignedStatus model:

public function status() {
    return $this->belongsTo('Status', 'status_id');
}

public function taskprogress() {
    return $this->hasMany('TaskProgress', 'assigned_status_id');
}

However when I use the taskprogress function I get multiple task_id's which i don't want, I want to know what status it is in at the current moment in time

I'm not sure how I should structure why query to do this, does anyone have any solutions to the problem that they could perhaps share with me? Sorry bit of a newbie here. Can this be done with Eloquent? or do I need to loop through the nested objects and unset taskprogress by the created_at date? Hope somebody can help.

A var_dump is available here in my dropbox

Example data is below, i have timestamps in all tables, created_at and updated_at but not displayed below:

task_progresses table

id | user_id | project_id | task_id | assigned_status_id
1  | 2       | 3          |  1      |    4
2  | 3       | 3          |  2      |    3
3  | 3       | 3          |  2      |    2
4  | 2       | 3          |  2      |    1
5  | 1       | 3          |  2      |    4
6  | 2       | 3          |  2      |    3
8  | 2       | 3          |  1      |    2
8  | 2       | 3          |  1      |    1

assigned_statuses

id | project_id | status_id 
1  | 2          | 1
2  | 2          | 3
3  | 2          | 4
4  | 2          | 2

statuses

id | name        |order |global
1  | Backlog     |0     | 1    
2  | Closed      |4     | 1
3  | In Progress |2     | 1
4  | Complete    |5     | 1
5  | Done        |3     | 0
6  | Open        |1     | 0

Aucun commentaire:

Enregistrer un commentaire