I have two tables:

**Items**

```
| id | name   | downloads | category_id |
|----|--------|-----------|-------------|
| 1  | Item 1 | 5         | 1           |
| 2  | Item 2 | 7         | 1           |
| 3  | Item 3 | 23        | 2           |

```

**Categories**

```
| id | name       |
|----|------------|
| 1  | Somecategory |
| 2  | Someothercategory |
| 3  | Somecategory2 |

```

I am currently using a basic query:

```
$cats = Category::where('name', 'like', '%'.$query.'%')
            ->orderBy('parent', 'desc')
            ->orderBy('name', 'asc')
            ->take(10)
            ->get();

```

But I would like the categories to be sorted by the number of their items' downloads. 

So if someone searches for "some" to return `Someothercategory` first, because its item (Item 3) has the most downloads.

Thanks!