Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wonder95's avatar

How to set $elements values in Laravel Slack notification block classes

I am converting a Laravel app from using Nathanhelfley/laravel-slack-blocks to using the built in Illuminate\Notifications\Slack\BlockKit\Blocks blocks, and I can't figure out how to pass in the $elements value. For instance, with a SlackBlock class, I would do this for a context block;

    $block->elements([
        [
            'type' => 'mrkdwn',
            'text' => "Pursuit: *{$this->activityModel->target->name}*",
        ],
    ]);

However, with the ContextBlock class, there is no interface to pass in the entire element (e.g. type => markdown) because there is no setter for $elements and it is protected. The same is true for the other block types. How do I pass in the other values for a block other than the text and the id?

Per LarryAI, the correct implementation is something like this

$contextBlock = new ContextBlock();
$contextBlock->text(new MarkdownText("Pursuit: *{$this->activityModel->target->name}*"));
Add the block to your Slack message:
use Illuminate\Notifications\Slack\SlackMessage;

$slackMessage = (new SlackMessage)
    ->blocks(function ($block) use ($contextBlock) {
        $block->context($contextBlock);
    });

But there are two problems

  • The MarkdownText class doesn't exist
  • the TextObject class does, and it has a markdown() setter. However, the text() method takes a string, not an object.

Something like this doesn't work, for the same reason

        $block = new ContextBlock();

        $text_block = new TextObject("Pursuit: *{'My pursuit text}*");
        $text_block->markdown();

        $block->text($text_block);

Yes, I've seen the Laravel Notifications docs, but that doesn't really answer this question, either (unless I'm missing something).

So how do I build the blocks with all of the $elements values?

0 likes
5 replies
LaryAI's avatar
Level 58

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:

  1. We create a ContextBlock instance.
  2. We create a TextObject with the desired text and set it to markdown using the markdown() method.
  3. We add the TextObject to the ContextBlock using the element() method.
  4. Finally, we add the ContextBlock to the SlackMessage using the blocks() method.

This should correctly set the $elements values for the ContextBlock in your Slack notification.

wonder95's avatar

Sorry, Larry, incorrect. As you can see here, there is no element() method on the ContextBlock class.

wonder95's avatar

Ok, for markdown, that's fine. but looking at all the other BlockKit classes, they all allow up to 25 elements, but there is no setter for the $elements property

Huholoman's avatar

@wonder95 I dont think you are supposed to set either elements or bloks manually. There are methods like headerBlock, contextBlock, actionsBlock,.. which create the block for you. The elements are restricted by the type of block, which would be nice if the laravel library would offer everything as like as slack offers it.

$msg = new SlackMessage()
    ->headerBlock('Ice-Cream Offer')
    ->contextBlock(fn (ContextBlock $block) => $block->text('Would you like some Ice-Creame?'))
    ->actionsBlock(function (ActionsBlock $block) {
       $block->button('Yes')->primary()->confirm();
       $block->button('No')->danger();
    );

But I have this issue with actionsBlock there is only button available altho Slack offers more elements to be in actions than just button.

Please or to participate in this conversation.