F.ture
F.ture

Reputation: 245

Using view share in blade

When displaying the code on view it gives an error . Which is:

Undefined variable: noti_count (View: C:\laravel\Procure\resources\views\layouts\user.blade.php)

I'm not yet used to use view share in controller . Need some guidance:

public function __construct()
{
    if (Auth::check())
    {
        if (Auth::User()->role == "Normal User")
        {       
            $noti_count = Notification
                              ::where('receive_id','=',Auth::user()->id)
                              ->count();

            view()->share('noti_count', $noti_count);

        }
        else if (Auth::User()->role == "PWD")
        {
            # code...
        }
        elseif (Auth::User()->role == "Senior Citizen")
        {
            # code...
        }
        else
        {       

        }    
    }    
}

Blade view code

<span class="badge badge-danger badge-counter">{!! $noti_count !!}+</span>

Upvotes: 0

Views: 4574

Answers (3)

Kenny Horna
Kenny Horna

Reputation: 14271

You can check the Sharing Data Will All Views section of the documentation. It says:

Sharing Data With All Views

Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method. Typically, you should place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('key', 'value');
    }
}

As you can see, this is helpful to share data accross multiple views. You can do it in your case of course (the documentation is very clear), but if you are gonna do some conditional stuff, why don't return data to the view instead? Like this:

public $data;

public function __construct()
{
    if ($condition)
    {
        $this->data = 'Data for first option';
    }
    else if ($condition_two)
    {
        $this->data = 'Some other data';
    }
    else
    {
        $this->data = 'Another amount of data';
    }
}

Then in a method inside the controller:

public function myMethod()
{
    // some stuff

    return view('my_view')->withData($this->data);
}

Upvotes: 1

MohamedSabil83
MohamedSabil83

Reputation: 1559

view()->share() should be used within a service provider's boot.

You can use AppServiceProvider, or, make a new provider and don't forget to add it to the end of providers array in config/app.php.

Notice that when no user is logged in, the $noti_count will be undefined. So, you have to check it first:

@isset($noti_count)
<span class="badge badge-danger badge-counter">{!! $noti_count !!}+</span>
@endisset

Upvotes: 4

Natvarsinh Parmar - bapu
Natvarsinh Parmar - bapu

Reputation: 1138

public function __construct()
{        
    \View::share('key','value');
}

Then in blade file:

<span class="badge badge-danger badge-counter">{{ $key }}</span>

Upvotes: -1

Related Questions