Laravel User Laravel User - 5 months ago 39
PHP Question

Laravel 5.2 : How to get a variable in a subView of a parentView from another subView?

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>

Answer

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)
Comments