burstofcode
4/18/2018 - 5:10 AM

Passing variable to the view

Three ways for passing variable to the view.

PATH: /routes/web.php

<?php

/*
    Option 1
*/
Route::get('/', function () {
    $name = 'Laracast';

    return view('welcome', compact('name'));
});


/*
    Option 2
*/
Route::get('/', function () {
    return view('welcome', [
        'name' => 'Laracast',
        'age' => 3,
    ]);
});


/*
    Option 3
*/
Route::get('/', function () {
    return view('welcome')->with('name', 'Laracast');
});