Answer the question
In order to leave comments, you need to log in
How to pass variable from Controller to View in Laravel?
Hello!
Using Composer installed the latest version of Laravel.
I ran into a banal problem ..
I want to transfer a variable to the View, but nothing happens.
In Controller ( HomeController.php ) specified the following:
public function showWelcome(){
$data['cheese'] = 'My pretty variable';
return View::make('hello', $data);
}
{{ $data['cheese'] }}
{{ $cheese }}
{{ cheese }}
<? echo $cheese; ?>
<? echo $data['cheese']; ?>
/*
Route::get('/', function()
{
return View::make('hello');
});
*/
Route::get('/', '[email protected]');
Answer the question
In order to leave comments, you need to log in
In general, you have some kind of anomaly there.
Works great:
public function showWelcome(){
$data['cheese'] = 'My pretty variable';
return View::make('hello', $data);
// or
return View::make('hello', ['cheese' => 'My pretty variable']);
// or
$cheese = 'My pretty variable';
return View::make('hello', compact('cheese'));
}
{{ $cheese }}
controller:
public function showWelcome()
{
$data['cheese'] = 'My pretty variable';
return View::make('hello', array(
'data' => $data,
);
}
public function showWelcome()
{
$data['cheese'] = 'My pretty variable';
return View::make('hello')->compact('data');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question