Ad

Passing Data To A View Laravel

- 1 answer

Just getting to mvc frameworks and im trying to pass data to my view using blade template engine.

here's my routes file

Route::get('/', '[email protected]');

my PagesController

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class PagesController extends Controller {

    public function index() {

        return view('index');

        $url = 'example.com';
    }

    public function about() {


        return 'hello';


    }

}

my index.blade.php file

<h1>{{ $url }}</h1>

I get the Undefined variable: url error message. What am i doing wrong?

Ad

Answer

Try this

public function index() {
   $url = 'example.com';
   return view('index', ['url' => $url]); 
}

Initialize the variable and send it as a parameter to the view.

Ad
source: stackoverflow.com
Ad