Sorting relationships

Posted 2 months ago by cservices

I have a query that looks like this:

$data['this_assets_tags'] = AssetTag::where("asset_id", $id)

Here is what it does: AssetTag (asset_tags) is a pivot table with only two columns. 'asset_id' and 'tag_id'.

First I get all the records for the asset id passed.

Next I get the tag data for each tag related to the asset

I then go back to the asset_tag table to get all the asset ids belonging to each found tag

Finally I get all the assets for each tag, excluding the asset id originally passed

Problem: The page lists the assets that belong to each tag. The boss wants me to prioritize the results by listing the tags according to the amount of assets descending.

Files Related By Tag:
Tag: Foo
 - An Asset title
 - Another Asset title
 - One more Asset title
Tag: Foobar
 - An Asset

Is there a way to do this without having to break the query apart, writing each relationship individually?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.