So you have a table, say messages, which has a json column type, group_ids that stores an array of group_ids... and you want to join to a groups table to get the $group->name?
Is my summary above accurate?
I'm not sure you'd be able to do that exactly as you have it defined.
Some possible avenues to explore however:
-
Move that field into a
group_messagepivot table that would contain amessage_idand agroup_id, and define a Many To Many relationship using abelongsToMany(Groups::class)and ahasMany(Messages::class)on your models. -
Use Caleb Porzio's Sushi package which allows you to treat an array like an Eloquent Model. It looks like you'd use the package's
public function getRows()method, and pass the rows into the Sushi model's constructor? The problem with this approach is I don't think it would work well for a collection of Messages because you'd probably have to send the group_ids for every Message in the collection to a separate GroupMessage sushi model's constructor. Anyways, here's a link to the repo if you want to do some research on that package: https://github.com/calebporzio/sushi
Ultimately, your best bet is create a pivot table, group_message, and to write a job to migrate all of your messages records' group_ids to new records in that pivot table.