renathy renathy - 1 year ago 154
PHP Question

Laravel: passing variables from Route::get static page to view

I am working with Laravel and I am not used to it. Currently I need to add description and title tags to page (that are missing from existing page already coded by someone else).

I see that there are some static pages that doesn't have any controller but are defined also in routes.php. For instnace, one of them is like this:

Route::get('feedback', function()
return view('feedback');

It renders feedback.blade which uses home.layout. I do not understand how to pass variables from this routes.php to home.layout.

If I would understand, I would be able to define some array like

and render those in layout.
It seems as basic question, but I really tried to google and no results.

I have tried like this:

Route::get('feedback', function()
$meta = [
'title' => 'About title',
'description' => 'About description'
return view('feedback', ['meta' => $meta]);

and in blade layout

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
{{ isset($meta) ? $meta->title : 'test' }}

But it resulted in error.

Answer Source

You can use some of the following options:

Route::get('feedback', function()
    return view('feedback')->with('variable', $variable);

Route::get('feedback', function()
    return view('feedback', ['variable' => $variable]);

Route::get('feedback', function()
    return view('feedback')->with(compact('variable'));

Then in the blade file you can use $variable as a local variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download