Skip to content
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

Add trait to return a models last activity as a relation #1213

Closed

Conversation

chrisrhymes
Copy link
Contributor

This adds a new trait that can be added to models that already use the activity log. The trait allows the model to have the last activity as a relation of the model or models.

This trait is inspired by this article by Jonathan Reinink.

$newsItem = NewsItem::withLastActivity()->first();

$newsItem->lastActivity // Activity model

Or multiple models.

$newsItems = NewsItem::withLastActivity()->get();

$newsItems[0]->lastActivity // Activity model

You can also specify the event type to return, such as 'created' or 'updated'.

$newsItem = NewsItem::withLastActivity('updated')->first();

@Gummibeer
Copy link
Collaborator

There's a special Laravel has one of many relationship which doesn't need any subquery or scopes.

@chrisrhymes
Copy link
Contributor Author

Thanks, I wasn't aware of the has one of many relationship. I updated the PR with this relationship but the tests are now failing. I think I'll just make this a trait in my own app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants