Laravel: can't pass variable to the view -
i have couple of blade pages using inheritance , trying show list of product in 1 page. when object passed view going wrong , it's crashing app.
the error getting undefined variable: batteries
.
with isset($batteries)
tested indeed variable not getting view.
here code:
/* controller */ class homecontroller extends \basecontroller { public function index() { $view = view::make('home.index')->render(); homecoming response::json(['view' => $view]); } public function main() { $batteries = battery::all(); $view = view::make('home.main', compact('batteries'))->render(); homecoming response::json(['view' => $view]); } }
views:
<!-- home/index.blade.php --> @extends('layouts.master') @section('content') @include('home.main') @stop <!-- home/main.blade.php --> <ul class="list-group"> @foreach ($batteries $battery) <li class="list-group-item">$battery->name</li> @endforeach </ul>
what wrong? why variable undefined?
edit
route::get('/', function() { homecoming view::make('home.index'); }); route::group(['before' => 'ajax'], function() { route::get('/home/main', ['as' => 'home.main', 'uses' => 'homecontroller@main']); });
the problem index
method display home.index
file include home.main
, in file utilize $batteries
variable.
you should not include home.main
template here or add together checking if $batteries
set:
@if (isset($batteries)) <ul class="list-group"> @foreach ($batteries $battery) <li class="list-group-item">{{{ $battery->name }}}</li> @endforeach </ul> @endif
to display name of $battery, should utilize {{{ $battery->name }}}
, not $battery->name
edit
the problem route, should utilize controller method:
route::get('/', ['uses' => 'homecontroller@index']);
and can alter index
method into:
$batteries = battery::all(); $view = view::make('home.main', compact('batteries'))->render(); homecoming response::json(['view' => $view]);
laravel laravel-4 laravel-routing
No comments:
Post a Comment