Reputation: 11
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 class="antialiased">
<div id="app">
<div class="main-wrapper">
@include('components.navbar')
@include('components.sidebar')
<!-- Main Content -->
<div class="main-content">
<section class="section">
<div class="section-header">
@isset($header_content)
{{ $header_content }}
@else
{{ __('Pages') }}
@endisset
</div>
<div class="section-body">
{{ $slot }}
</div>
</section>
</div>
</div>
</div>
@stack('modals')
@livewireScripts
@isset($script)
{{ $script }}
@endisset
The Sidebar
<ul class="sidebar-menu">
<li class="{{ $currentAdminMenu == 'dashboard' ? $activeClass : '' }}"><a class="nav-link"
href="{{ url('/dashboard') }}"><i class="fas fa-fire"></i> <span>Dashboard</span></a></li>
@foreach ($moduleAdminMenus as $moduleAdminMenu)
<li class="menu-header">{{ $moduleAdminMenu['module'] }}</li>
@foreach ($moduleAdminMenu['admin_menus'] as $moduleMenu)
@can($moduleMenu['permission'])
<li class="{{ $currentAdminMenu == strtolower($moduleMenu['name']) ? $activeClass : '' }}"><a
class="nav-link" href="{{ url($moduleMenu['route']) }}"><i
class="{{ $moduleMenu['icon'] }}"></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 class="section-header-breadcrumb">
<div class="breadcrumb-item active"><a href="#">Dashboard</a></div>
<div class="breadcrumb-item"><a href="#">Layout</a></div>
<div class="breadcrumb-item">Default Layout</div>
</div>
</x-slot>
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<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
Upvotes: 1
Views: 6258
Reputation: 102
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
Upvotes: 1
Reputation: 66
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
Upvotes: 0