PHP code example of xetaio / xetaravel-mentions

1. Go to this page and download the library: Download xetaio/xetaravel-mentions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xetaio / xetaravel-mentions example snippets


> 'providers' => [
>   //...
>   Xetaio\Mentions\Providers\MentionServiceProvider::class,
>   //...
> ]
> 

> php artisan vendor:publish --provider="Xetaio\Mentions\Providers\MentionServiceProvider"
> 

> 
>
> return [
>     'pools' => [
>         // Here you configure as many pools as you want. But basically we
>         // notify only the Users.
>         'users' => [
>             // Model that will be mentioned.
>             'model' => App\Models\User::class,
>
>             // The column that will be used to search the model by the parser.
>             'column' => 'username',
>
>             // The route used to generate the user link.
>             'route' => '/users/profile/@',
>
>             // Notification class to use when this model is mentioned.
>             'notification' => App\Notifications\MentionNotification::class,
>         ]
>     ]
> ];
> 

> 
> namespace App\Models;
>
> use Illuminate\Database\Eloquent\Model;
> use Xetaio\Mentions\Models\Traits\HasMentionsTrait;
>
> class Comment extends Model
> {
>     use HasMentionsTrait;
> }
> 

> 
> namespace App\Http\Controllers;
>
> use Xetaio\Mentions\Parser\MentionParser;
>
> class CommentController extends Controller
> {
>      public function create(Request $request)
>      {
>          // Handle the comment creation however you like
>          $comment = Comment::create($request->all());
>
>          // Register a new Parser and parse the content.
>          $parser = new MentionParser($comment);
>          $content = $parser->parse($comment->content);
>
>          /**
>           * Re-assign the parsed content and save it.
>           *
>           * Note : If you use a custom Parser and you don't modify
>           * the `$content` in your custom Parser, you can ignore that.
>           */
>          $comment->content = $content;
>          $comment->save();
>      }
> }
> 

> 
>
> [
>      // The pool used with the parser.
>      'pool' => 'users',
>
>      // If `false`, the parser won't mention the user.
>      'mention' => true,
>
>      /**
>       * If `false` the parser won't notify the user.
>       *
>       * Note : If the `mention` option is set to `false`, this setting is ignored.
>       */
>      'notify' => true,
>
>      /**
>       * The character that will trigger the mention.
>       * Note : If you modify this setting, you will also need to modify
>       * the `regex_replacement.{character}` option to match this setting.
>       */
>      'character' => '@',
>
>      // The regex used to parse the mentions.
>      'regex' => '/\s({character}{pattern}{rules})/',
>
>      /**
>       * The replacement used to replace the regex pattarn.
>       * Can be usefull if you want to allow a special character in the mention like `_` or `-`
>       * or pass dynamic value to the regex.
>       *
>       * Note : The parse use the PHP function `strtr` to replace the pattarn in the regex.
>       */
>      'regex_replacement' => [
>          '{character}'  => '@',
>          '{pattern}'  => '[A-Za-z0-9]',
>          '{rules}'  => '{4,20}'
>     ]
> ]
> 

> 
>
> $parser = new MentionParser($comment, [
>     'pool' => 'members',
>     'notify' => false
> ]);
> 

> 
>
> $parser = new MentionParser($comment);
> $parser->setOption('notify', false);
> $content = $parser->parse($comment->content);
> 

> 
>
> $value = $parser->getOption('notify');
> // false
> 

> 
> namespace App\Parser;
>
> use Illuminate\Support\Str;
> use Xetaio\Mentions\Parser\MentionParser;
>
> class CustomParser extends MentionParser
> {
>
>     protected function replace(array $match): string
>     {
>         $character = $this->getOption('character');
>         $mention = Str::title(str_replace($character, '', trim($match[0])));
>
>         $route = config('mentions.pools.' . $this->getOption('pool') . '.route');
>
>         $link = $route . $mention;
>
>         // Here we return a HTML link instead of the default Markdown.
>         return " <a class=\"link\" href=\"{$link} \">{$character}{$mention}</a>";
>     }
> }
> 

> 
>
> $parser = new \App\Parser\CustomParser($comment);
> $content = $parser->parse($comment->content);
> 

> 
> namespace App\Notifications;
>
> use Illuminate\Bus\Queueable;
> use Illuminate\Notifications\Notification;
>
> class MentionNotification extends Notification
> {
>     use Queueable;
>
>     /**
>      * The Comment instance.
>      *
>      * @var \Illuminate\Database\Eloquent\Model
>      */
>     public $model;
>
>     /**
>      * Create a new notification instance.
>      *
>      * @param \Illuminate\Database\Eloquent\Model $model
>      */
>     public function __construct($model)
>     {
>         $this->model = $model;
>     }
>
>     /**
>      * Get the notification's delivery channels.
>      *
>      * @param mixed $notifiable
>      *
>      * @return array
>      */
>     public function via($notifiable): array
>     {
>         return ['database'];
>     }
>
>     /**
>      * Get the array representation of the notification.
>      *
>      * @param mixed $notifiable
>      *
>      * @return array
>      */
>     public function toDatabase($notifiable): array
>     {
>         // The instance `$this->model` represent the `Comment` model.
>         $username = $this->model->user->username;
>         $modelId = $this->model->getKey();
>
>         $message = "<strong>@{ $username }</strong> has mentionned your name in his comment !";
>
>         // You could (and probably should) use a route name here with the function `route()`.
>         $link = "/comment/show/{ $modelId }";
>
>         return [
>             'message' => $message,
>             'link' => $link,
>             'type' => 'mention'
>         ];
>     }
> }
> 
sh
> php artisan migrate
>