2
votes

I have a partial page "sidebar" that shows up in every page. There my logo is dynamic. To display my logo i have send variable through my dashboard controller but it says undefined variable. The problem seems to be in route because "/dashboard" is being called not the "sidebar" For sidebar i have wrote its part in dashboard controller So, how can i make this work?

Undefined variable: logos (View: C:\xampp\htdocs\odan\resources\views\admin\partials\sidebar\sidebar.blade.php) (View: C:\xampp\htdocs\odan\resources\views\admin\partials\sidebar\sidebar.blade.php) (View: C:\xampp\htdocs\odan\resources\views\admin\partials\sidebar\sidebar.blade.php)

Dashboard controller

public function sidebar()
{
    $data['logos'] = Setting::first();

    return view('admin.layouts.app', $data);
}

Sidebar.blade.php

<!-- Brand Logo -->
  <a href="index3.html" class="brand-link">
    <img src="{{asset('uploaded/'.optional($logos)->image)}}" alt="Odan Logo" 
     class="brand-image img-circle elevation-3" style="opacity:.8">
    <span class="brand-text font-weight-light">Odan</span>
  </a>

app.blade.php

<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
  @include('admin.partials.navigation.navbar')
  @include('admin.partials.sidebar.sidebar')
  @yield('content')
  @include('admin.partials.footer.footbar')
</div>
3
Do you have the view controller for sidebar and dashboard in the same file? - Udo E.
@UdoE. yes sidebar() function in same dashboard controller - user10060106

3 Answers

2
votes

The $data you pass to admin.layouts.app does not exists in sidebar

When you @include a view from within a view, the framework is compiling a new view and the $data you passed to the first view does not persist to the new view, this is called a shared view.

There are a few solutions for your problem, from fastest to finest:

1) passing it directly to the shared view

@include('admin.partials.sidebar.sidebar', ['logos' => $logos])

This will "share" the data the shared view needs. It goes without saying this is not the best solution.

2) the ->share() method

In your controller, do this:

view()->share('logos', Setting::first());

This will create the $logos variable in all the views, including the shared views. Place the call inside a service provider maybe and you won't have duplicated code.

3) View Composers

This is the extra fine solution, if you have an interest in it, please read this article on View Composers

Hope it helps.

1
votes

Sidebar called in every blade file so you supposed to call helper function for it. Change in your composer.json file:

"autoload": {
    "files": [
        "app/helpers.php"
    ],
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

Run:

composer dump-autoload

Create helpers.php file in your app folder.

And write your function in helpers.php like.

function sidebar()
{
    $data['logos'] = Setting::first();

    return $data;
}

And call that function in sidebar.php

0
votes

You can use the Provider and Composer to achieve your goal

Step One

First Create a Composer class in App\Http\View\composers directory If you already don't have this directory you can create one

There create a File SidebarComposer There you have to assign your data to compose method. Below I share a full composer code

<?php
namespace App\Http\View\composers;

use Illuminate\View\View;
use App\Models\SideMenu;

class SidebarComposer
{
    protected $sidemenuItems;

    public function __construct(SideMenu $SideMenu)
    {
        $this->sidemenuItems = $SideMenu::getAll();
    }

    public function compose(View $view)
    {
        $view->with('menuItems', $this->sidemenuItems);
    }
}

Here I call Model to get data but you can call anything you like your controller or repository

Step Two:

Create a service provider. You can create it Manually but I would suggest to do it by command

php artisan make:provider SidebarServiceProvider

Then register your service provider in config/app.php

Your service provider, you will find boot Method there you have to call view::composer to pass your data into your view

public function boot()
{
    View::composer('incl.sidebar', SidebarComposer::class);
}