anyone I got stuck when i try to pass data from the controller to the view blade. I am using laravel jetstream as the starter of my project and here is my code
The Dashboard Controller
public function index()
{
$this->data['currentAdminMenu'] = 'dashboard';
return view('dashboard', $this->data);
}
Main Layout
<body >
<div id="app">
<div >
@include('components.navbar')
@include('components.sidebar')
<!-- Main Content -->
<div >
<section >
<div >
@isset($header_content)
{{ $header_content }}
@else
{{ __('Pages') }}
@endisset
</div>
<div >
{{ $slot }}
</div>
</section>
</div>
</div>
</div>
@stack('modals')
@livewireScripts
@isset($script)
{{ $script }}
@endisset
The Sidebar
<ul >
<li ><a
href="{{ url('/dashboard') }}"><i ></i> <span>Dashboard</span></a></li>
@foreach ($moduleAdminMenus as $moduleAdminMenu)
<li >{{ $moduleAdminMenu['module'] }}</li>
@foreach ($moduleAdminMenu['admin_menus'] as $moduleMenu)
@can($moduleMenu['permission'])
<li ><a
href="{{ url($moduleMenu['route']) }}"><i
></i> <span>{{ $moduleMenu['name'] }}</span></a>
</li>
@endcan
@endforeach
@endforeach
</ul>
The Dashboard View
<x-app-layout>
<x-slot name="header_content">
<h1>Dashboard</h1>
<div >
<div ><a href="#">Dashboard</a></div>
<div ><a href="#">Layout</a></div>
<div >Default Layout</div>
</div>
</x-slot>
<div >
<x-jet-welcome />
</div>
when I am try run the code I got an error message like this Undefined variable $currentAdminMenu. but if I change the dashboard view with the @extend layout there is no error message anymore.
The question is how to pass $this->data from the controller to the dashboard view using <x-app-layout>. Could anyone here give me any suggestions or help me to solve this problem?
Thank you for your help
CodePudding user response:
Passing data into blade template
public function index()
{
$this->data['currentAdminMenu'] = 'dashboard';
return view('dashboard', [
'data' => $this->data
]);
// OR
return view('dashboard')->with('data', $this->data);
}
variable by the name of data can be access inside blade
CodePudding user response:
Based on the docs it should be a key value pair when passing data to view method. See https://laravel.com/docs/8.x/views#passing-data-to-views
