I have a partial view in master layout which is the navigation bar. I have a variable $userApps
. This variable checks if the user has enabled apps (true), if enabled then I would like to display the link to the app in the navigation bar.
homepage extends master.layout which includes partials.navbar
My code in the navbar.blade.php is this:
@if ($userApps)
// display link
@endif
However I get an undefined variable error. If I use this in a normal view with a controller it works fine after I declare the variable and route the controller to the view. I dont think I can put a controller to a layout since I cant route a controller to a partial view, so how do I elegantly do this?
What version of Laravel you use? Should be something like this:
@include('partials.navbar', ['userApps' => $userApps])
Just for test I did it locally and it works:
routes.php
Route::get('/', function () {
// passing variable to view
return view('welcome')->with(
['var' => true]
);
});
resources/views/welcome.blade.php
// extanding layout
@extends('layouts.default')
resources/views/layouts/default.blade.php
// including partial and passing variable
@include('partials.navbar', ['var' => $var])
resources/views/partials/navbar.blade.php
// working with variable
@if ($var == 'testing')
<h1>Navbar</h1>
@endif
So problem must be in something else... check paths and variable names.
This approach is very simple:
In parent view :
@include('partial.sub_view1', [$var1='This is value1'])
In sub view :
{{ $var1 }}
@include('your view address', array('variable in your view for example: id=$something ' => 'value for this variable'))