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

jgravois's avatar

Laravel 7 Component showing <?php echo e(session('toastr')['body']); ?>

I am following the What's New in Laravel 7: Supercharged Blade Components video, I created a Laravel 7 component using php artisan make:component Toastr and created the component (WIP). I am not sure what I am doing wrong.

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Toastr extends Component
{
    public $body;
    public $type;

    public function __construct($type, $body)
    {
        $this->body = $body;
        $this->type = $type;
    }

    public function render()
    {
        return view('components.toastr');
    }
}

The component view looks like this

<div class="absolute right-0 mt-4 mr-4 flex justify-between items-center font-semibold p-4 text-xl text-white bg-blue-500 rounded-lg">
    <span class="mr-4 text-3xl">
        @svg('uam/uam-tail')
    </span>

    {{ $body }}
</div>

In a Laravel-Livewire component, I am calling it

public function clearCache()
    {
        Artisan::call('cache:clear');
        Session::flash('toastr', ['body'=>'Artisan Command completed!','type'=>'info']);
    } // end function

and the Laravel-Livewire component view is

{{--        @if (session()->has('toastr'))--}}
        <x-toastr
            type="{{session('toastr')['type']}}"
            body="{{session('toastr')['body']}}"></x-toastr>
{{--        @endif--}}
0 likes
3 replies
bobbybouwmann's avatar

The problem is probably in the commented out blade. That isn't compiled correctly here. If you remove the commented @if and @endif you should be good to go ;)

jgravois's avatar

I removed the comments which I had just added for debugging and the display hasn't changed.

screenshot

AlexMartinFR's avatar

I think you used the wrong syntax in your Laravel-Livewire component view, try this:

{{--        @if (session()->has('toastr'))--}}
        <x-toastr
            :type="session('toastr')['type']"
            :body="session('toastr')['body']"></x-toastr>
{{--        @endif--}}

You need to prefix your attributes names with a colon if you want them to evaluate PHP code.

Check the documentation about passing data to your components for more explanation: https://laravel.com/docs/master/blade#passing-data-to-components

Please or to participate in this conversation.