mercredi 15 juin 2016

Return Models based on Query

I am trying to get results based on a query but not sure how to do it this way. I have Projects which belong to a User. So a Project has a user_id.
A user then belongs to a department so the user table has a department_id.

If I pull the results of a Project I see something like this.

id: 3
projectName: This is a project
user_id: 99
deleted_at: 2016-06-15 09:28:00
created_at: 2016-06-14 14:34:16
updated_at: 2016-06-15 09:28:00

If I pull a users data I see

id: 99
userName: Some Name
userEmail: email@email.com
active: 1
department_id: 4
created_at: 2016-06-13 09:56:59
updated_at: 2016-06-13 12:16:28

Lastly, if I check the departments I get

id: 4
departmentName: Department A
created_at: 2016-06-09 12:09:43
updated_at: 2016-06-15 08:03:45

Within my Controller I can do the following to get all projects

$projects = Project::dateDescending()->get();

However, this time I only want to get projects that were made by users of Department A. At the moment I am doing

$projects = Project::dateDescending()->get();
if(!empty($projects)) {
    foreach ($projects as $project) {
        dd($project->user->department->where('departmentName', 'Department A'));
    }
}

The problem with the above is that it returns a department object, not the Project object. So my plan is to get all projects made by a user who is in Department A. How would I go about doing this?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire