3

Below is my views and controllers i want $name variable to be accessible in navbar view which is included in master view. does anybody know a solution ?

userController.php

public function index($var){
    $u_array = $var;
    $name = $u_array->name;
    return view('index',compact(name));
}

master.blade.php

@include('navbar')
<section class="main-container">
    @yield('content')
</section>

index.blade.php

@extends('master')
@section('content')
    <h2>{{$name}}</h2>
@endsection

navbar.blade.php

<h1>{{$name}}</h1>
tereško
  • 58,060
  • 25
  • 98
  • 150
Laravel User
  • 1,111
  • 1
  • 8
  • 24
  • Does {{name}} display anything where it is currently? –  Jun 30 '16 at 13:50
  • yes it is displaying name from database. – Laravel User Jun 30 '16 at 13:51
  • Deleted my answer as it was not helpful to you. –  Jun 30 '16 at 14:17
  • Maybe open in the nav and then make a call to the DB from there. Hope you figure it out –  Jun 30 '16 at 14:18
  • Are you attempting to get the authorized user's name? – camelCase Jun 30 '16 at 14:21
  • I notice you have `{{ name }}` in `index`, but it should be `{{ $name }}`. Since `index.blade.php` extends `master.blade.php`, then anything in `index` is also available in `master`, so it should be working. – camelCase Jun 30 '16 at 14:35
  • Typing error corrected – Laravel User Jun 30 '16 at 14:42
  • No, its some variable i need to get in navbar view – Laravel User Jun 30 '16 at 14:43
  • @davin i need via blade template engine not from php code as i said in question. – Laravel User Jun 30 '16 at 14:44
  • If `$name` is defined in the `userController.php` file, then what you have above should be working. I use this exact concept all over my applications. Perhaps try passing it _without_ `compact`, using just a plain array: `return view('index', ['name' => $name]);`. – camelCase Jun 30 '16 at 14:51
  • Take a look here: [pass data to all views](http://stackoverflow.com/questions/35040684/how-to-share-a-variable-across-all-views/35042335#35042335) – Panagiotis Koursaris Jun 30 '16 at 14:51
  • @camelCase still not working – Laravel User Jun 30 '16 at 14:53
  • And if you `dd($name)` in your controller, it is defined? – camelCase Jun 30 '16 at 14:54
  • @koursaris not usefull for single view scope. I want to extend the scope of name variable to navbar view only – Laravel User Jun 30 '16 at 14:56
  • @camelcase it is defined. And working for index view – Laravel User Jun 30 '16 at 14:57
  • Then I'm not sure because it should be working. Perhaps you should show us the code you are using in `navbar` since the issue is with that file. – camelCase Jun 30 '16 at 15:02
  • @camelcase i know that the scope of name variable is limited to the view it is passed to from laravel docs. That is what i need to do extend the scope of name variable to navbar view. But anyway thanks for your help. – Laravel User Jun 30 '16 at 15:06
  • No, it isn't limited. Read the section *Including sub-views* [here](https://laravel.com/docs/5.2/blade#control-structures), where it states: "Blade's @include directive, allows you to easily include a Blade view from within an existing view. All variables that are available to the *parent view* will be made available to the *included view*" which is what you are doing, right? – camelCase Jun 30 '16 at 15:13
  • Yes . But see my case . I have the variable in a sub view of Parent view master. And i need that variable of sub view index in other subview navbar of parent master view. – Laravel User Jun 30 '16 at 15:38
  • 1
    Here, see [this image](http://imgur.com/7h1jgF8) of your case, working on my local machine. Is this not identical to what you have above? – camelCase Jun 30 '16 at 15:59
  • Yes this is similar to what i have done. But $name is not found in navbar view. – Laravel User Jun 30 '16 at 16:28

3 Answers3

3

Pass the variable to master.blade then navbar.blade:

@extends('master', ['name' => $name]) //compact('name')

Then:

@include('navbar', ['name' => $name]) //compact('name')
Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
2

you can use @section in index view and @yield in navbar view as

index.blade.php

@extends('master')
@section('content')
    <h2>{{$name}}</h2>
@endsection
@section('nav')
    <h1>{{$name}}</h1>
@endsection

navbar.blade.php

@yield(nav)
Veer Singh
  • 36
  • 4
0

You should use a view composer for this.

View Composer

view()->composer('master', function ($view) {
  $u_array = $var;
  $name = $u_array->name;
  $view->with(compact(name));
});

With some more explanation on your specific issue I may be able to assist you in better alternatives.

ExohJosh
  • 1,832
  • 16
  • 22
  • I have passed name variable to index view from controller which i need in navbar view as name variable via blade template engine. – Laravel User Jun 30 '16 at 14:48