›
byrcsc/laravel-mentions · 1.x
Query targets, source models, and mention records through Eloquent relations and scopes.
Start with the result you need. You can load the models named by a comment, inspect the stored mention rows, or find every source model that names one target.
Return an Eloquent collection of target models:
$targets = $comment->mentioned();mentioned() queries the source's mention rows with their polymorphic target.
It filters missing targets and returns a new collection containing the models.
Use the relationship when you also need record fields:
$mentions = $comment->mentions()
->with('target')
->latest()
->get();HasMentions adds the whereMentions() local scope:
$comments = Comment::query()
->whereMentions($jane)
->latest()
->get();The scope compares the target's morph class and key. Equal IDs from different target classes do not collide.
Mentionable adds mentionedIn():
$mentions = $jane->mentionedIn()->get();The result can contain several source model types. Eager-load source before
iterating:
$mentions = $jane->mentionedIn()
->with('source')
->get();
foreach ($mentions as $mention) {
$source = $mention->source;
}Eloquent issues one query for the mention records and one query per source morph type represented in the result.
Use fromSourceType() on a mention query:
$commentMentions = $jane->mentionedIn()
->fromSourceType(Comment::class)
->with('source')
->get();The scope accepts a model class or a morph-map alias string. When passed a model class, it creates an instance and reads its morph class.
The configured table and model class also apply to direct queries resolved through the container:
use Byrcsc\Mentions\Models\Mention;
$mentions = app(Mention::class)
->newQuery()
->fromSourceType(Comment::class)
->with(['source', 'target'])
->get();Resolve the base class through the container when mentions.model may contain
a custom subclass.