To set the $elements values in Laravel's built-in Slack notification block classes, you need to use the appropriate methods provided by the Illuminate\Notifications\Slack\BlockKit\Blocks classes. Here's how you can achieve this using the ContextBlock and TextObject classes.
First, ensure you have the necessary imports:
use Illuminate\Notifications\Slack\SlackMessage;
use Illuminate\Notifications\Slack\BlockKit\Blocks\ContextBlock;
use Illuminate\Notifications\Slack\BlockKit\CompositionObjects\TextObject;
Then, you can create a ContextBlock and add a TextObject to it. The TextObject can be set to markdown using the markdown() method. Here's a complete example:
$contextBlock = new ContextBlock();
// Create a TextObject with markdown type
$textObject = (new TextObject('Pursuit: *' . $this->activityModel->target->name . '*'))->markdown();
// Add the TextObject to the ContextBlock
$contextBlock->element($textObject);
// Create the Slack message and add the ContextBlock
$slackMessage = (new SlackMessage)
->blocks(function ($block) use ($contextBlock) {
$block->context($contextBlock);
});
In this example:
- We create a
ContextBlockinstance. - We create a
TextObjectwith the desired text and set it to markdown using themarkdown()method. - We add the
TextObjectto theContextBlockusing theelement()method. - Finally, we add the
ContextBlockto theSlackMessageusing theblocks()method.
This should correctly set the $elements values for the ContextBlock in your Slack notification.