Laravel 4 Blade @include variable

user1995781 picture user1995781 · Jan 17, 2014 · Viewed 32.1k times · Source

I was trying to do include with Laravel blade, but the problem is it can't pass the variable. Here's my example code:

file_include.blade.php

<?php
  $myvar = "some text";

main.blade.php

@include('file_include')
{{$myvar}}

When I run the file, it return the error "Undefined variable: myvar". So, how can I pass the variable from the include file to the main file?

Thank you.

Answer

Kenny picture Kenny · Mar 17, 2014

Why would you pass it from the include to the calling template? If you need it in the calling template, create it there, then pass it into the included template like this:

@include('view.name', array('some'=>'data'))

Above code snippet from http://laravel.com/docs/templates