I have struggled with this a few time and am sure there must be a Laravel way of doing this.
I have a collection of tokens which are assigned to a user. The tokens table has a user_id in it and $token->user gives me a User model (there is a belongsTo relationship).
I now want to get a collection of all the users, how should I do this? I currently pluck all the user_ids to an array like this:
If you want all of the users related to a collection of tokens in a flat collection, then your current approach is just fine - it is efficient.
If you were to use a relationship, each Token instance in the collection will have its own users attribute which will be a Collection of User instances; this would require to be flattened in PHP.
@phpmick my reply was based on the original OP. The advice remains to make a new query with the user_id plucked from each Token instance - it is the same query Laravel would run for eager-loading without the PHP overhead of assigning the resulting User instances to each Token instance.