-
-
Notifications
You must be signed in to change notification settings - Fork 717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Searching for activity on both a model and a related model #526
Comments
As expected, no sooner than I posted this, I realized what I needed to do lol. For posterity:
Will close this out now :) |
Ok, I'm reopening this (sorry!), as I realized that my solution only works for first |
Hey, because I think that I got what you want I skipped after
If I'm wrong please forgive me and I will read all the rest. 😄 I had no idea how often I would suggest this as the solution to go the moment I developed it but again I will suggest the The way I would do this is to append a custom property to the public function tapActivity(Activity $activity, string $eventName)
{
$activity->withProperty('sku_name', $this->name); // in SKU model
$activity->withProperty('sku_name', $this->sku->name); // in Listing model
} If you are on MySQL you can query the activity model by json child (check that the Activity::where('properties->sku_name', $sku->name)->get(); Just as a suggestion: I would go with the I hope that this helps you!? |
@Gummibeer That looks like it will work nicely! Thanks for the suggestion. Off to give it a whirl :) Leaving this open for now, but will close if I don't have anything else to add to this. |
Because you got to a solution without any adjustments I will also refactor your last code snippet (blind shot - untested). $skus = Sku::where('name', 'LIKE', '%' . $searchTerm . '%')->get();
$query = Activity::forSubject($sku)->orWhere(function($query) use ($skus) {
$query
->where('subject_type', (new Listing())->getMorphClass())
->whereIn('subject_id', $skus->pluck('listings')->flatten(1)->pluck('id'));
}); The part where I pluck and flatten and pluck is the part where I'm unsure. Could be that |
Hmm. Does
Getting |
Foo - I'm sorry, the $activity->properties = $activity->properties->put('sku_name', $sku->name); This should do the trick. |
Yep, that did the trick! Also, messing around with your rewritten code above. The |
In this case duplicate the But I'm happy that you got something working. |
Great package! Definitely a timesaver that adds a tremendous amount of value. :) This may very well be more of an Eloquent question, but I'm not sure, as this is the first time I've dealt with polymorphic classes. I've been pouring through the documentation and past issues to try and find a solution to this. It seems simple enough, but my brain is befuddled at the moment. :)
So I have these two models set up, both of which log activity:
Sku
can have manyListings
, and aListing
can have only 1Sku
per.Both models are set up with the appropriate relationship methods as well, so:
$sku->listings()
is ahasMany
toListing
and$listing->sku()
is thebelongsTo
toSku
The tables are related in the usual way:
skus.id
is a foreign key here:listings.sku_id
What I want to do is to perform a
LIKE
search against thesku.name
column, but hit bothSku
records as well asListing
records for matchingSku
s.Example: Let's say a given
Sku
has 2Listing
records related to it. What I want to do is pick up activity for any changes to theSku
model, as well as any relatedListing
models.So, I need to search the equivalent of
$sku->name
as well as$listing->sku->name
.I was fiddling around with
has
andwhereHas
, but sadly, they do not supportMorphTo
relationships :(I could do this with raw DB statements and some logic around it, but obviously I would rather keep things expressive. This view also has pagination, so I need to paginate the results.
Something like this (though this won't work, of course):
I would greatly appreciate any clear set of eyes and thoughts on this. My brain is a wee bit frazzled at the moment :) Thanks in advance!
The text was updated successfully, but these errors were encountered: