Reputation: 1036
So this one has kinda given me a mental run around. I have Row
s, User
s, and RowActivity
models. Each time a user interacts with a row, it generates a single line in the row activity table saying what status the user changed. Each row can have 3-8 activity entries with a single user reference per line. So my question is this: I want to analyze the rows that particular users have interacted with. So I'd like to search for user A and get all the rows they've touched. But the only way to do that is to query the RowActivity
table.
So the query would essentially be this:
Row::whereHas('rowActivity')->whereWithin(Collection of RowActivity, user_id = requested-user)->get();
I know that I can go the long way and query the RowActivity::where('user_id', $requestedUser)
and then get all the other row activities based on the related row_id
of that those results, but I feel there's a clean way that I can't figure out.
Just for clarification, the row activities are used to generate reports about which users changed the status of the row and how long the duration between the changes are. So I need to get all the activities associated with a row as well as all the rows that a user has touched so that I can analyze how long their portion of that interaction took.
If I need to do this as a multilevel query, so be it, I don't have an aversion to that, I just want to make sure my queries are pristine. If I did it as multi-level, it would look something like this:
$ra = RowActivity::where('user_id', $requestedUser)->pluck('row_id');
$rows = Row::with('rowActivity')->find($ra); //get all rows and their associated activities based on the plucked row id from query 1
Upvotes: 0
Views: 33
Reputation: 5715
You can do that within whereHas()
. I hope that's what you want
Row::whereHas('rowActivity', function($query) use($requestedUser) {
$query->where('user_id', $requestedUser);
})->get();
Upvotes: 1