›
›
›
  1. docs
  2. ›
  3. byrcsc/laravel-mentions
1.x
Browse documentationOpenClose

Getting started

  • Introduction
  • Installation and setup
  • Quick start

Core concepts

  • Mention records
  • Parsing text
  • Resolving targets
  • Synchronization

Operations

  • Scan multiple attributes
  • Use markup mentions
  • Mention groups
  • Querying mentions
  • React to lifecycle events
  • Control synchronization
  • Extend the package

Reference

  • Configuration
  • Public API
  • Published assets
  • Testing
  • Troubleshooting

Getting started

  • Introduction
  • Installation and setup
  • Quick start

Core concepts

  • Mention records
  • Parsing text
  • Resolving targets
  • Synchronization

Operations

  • Scan multiple attributes
  • Use markup mentions
  • Mention groups
  • Querying mentions
  • React to lifecycle events
  • Control synchronization
  • Extend the package

Reference

  • Configuration
  • Public API
  • Published assets
  • Testing
  • Troubleshooting

byrcsc/laravel-mentions · 1.x

Querying mentions.

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.

Load targets from a source

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();

Find sources that mention a target

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.

Load records from a target

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.

Filter one source type

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.

Query the mention model directly

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.

What to read next

  • Mention records for the fields returned by each query.
  • Mention groups to use the same queries with teams.
  • Public API for relationship and scope signatures.
PreviousMention groupsNextReact to lifecycle events
View source

On this page

  1. Load targets from a source
  2. Find sources that mention a target
  3. Load records from a target
  4. Filter one source type
  5. Query the mention model directly
  6. What to read next