Eloquent nested WHERE statement

Viewed 1276

For an online competition with the goal of photographing certain objects I've got users uploading pictures of objects, where the objects have a category and a subcategory.

A user's dashboard should look something like this:

  ------------------------------------------------ 
  |                Category                      |  
  ------------------------------------------------  
  |   -----------------------------------------  |
  |   | Subcat.          |  Subcat.           |  |
  |   |------------------|--------------------|  |
  |   |Object1 | Object2 | Object1 | Object2  |  |
  |   |------------------|--------------------|  |
  |   |  Pic1  |  Pic1   | Pic1    | Pic1     |  |
  |   |  Pic2  |  Pic2   | Pic2    | Pic2     |  |
  |   ----------------------------------------   |
  |                                              |
  ------------------------------------------------  

In order to be able to output the pictures in the appropriate loop, I installed hasMany relationships between the models. So I can get an object with a convenient hierarchy by

$userPics = Category::with(['subcats','subcats.objects','subcats.objects.pics'])->get();

QUESTION:

For the dashboard of course I only want the images from a certain user: the Pictures model contains a user_id field. I could get the user's pictures directly: Pictures::where('user_id',$user->id)->get();, but then I don't have the structure in the object to create the desired view shown above.

How can I do something like this:

$userPics = Category::with([...])->where('user_id',$user->id)->get();

Many thanks for any tips!

1 Answers
Related