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
有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top