Question

I was trying to use the Blade template engine and got this errors

My Controller

<?php

class HomeController extends BaseController
{ 
    protected $layout = 'home';

    public function index()
    {        
        $data = array(
            'heading' => 'Hello Laravel (from Home)',
            'body' => 'This is awesome, from the HomeController'
        );

        $this->layout->content = View::make('home',compact('data'));
    }
}

View - [Try 1]

...
    @section('message')
        {{$data}}
    @stop

ERROR

ErrorException
Array to string conversion

View - [Try 2]

....
    @section('message')
        <?php print_r($data); ?>
    @stop

ERROR

ErrorException
Undefined variable: data
Was it helpful?

Solution

The issue is with how you're defining the layout and the view.

You're defining the layout of the controller as 'home' and then rendering the view 'home' as the content of the layout, which basically means you're rendering the same file again as the content. You should have a separate file for the layout. You could have View::make('index', $data) and make a file called index.blade.php. See here for more information

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top