Boubou
211
9
Laravel

About view composers - Cannot get it to work - Laravel 5.5

Posted 1 year ago by Boubou

In one of my previous thread Snapey told me about view composers, I have been trying to get it to work but something is not working as expected.... I reopened a new thread not to mix this particular problem with the previous query that was set as "answered".

Using Laravel 5.5

I have created a folder:

Http/ViewComposers

Inside this folder I have added:

Http/ViewComposers/NavbarBackendComposer.php

I have registered the class in the config/app.php as:

        App\Http\ViewComposers\NavbarBackendComposer::class,

I have also added this in the App/provider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

        View::composer(
            'layout.backend', 'App\Http\ViewComposers\NavbarBackendComposer'
        );


        Schema::defaultStringLength(191);
    }

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

Now in the NavbarBackendComposer.php file I have added this:

<?php

namespace App\Http\ViewComposers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;


class NavbarBackend
{



    public function __construct()
    {


    }




    public function compose(View $view)
    {

        $count = DB::table('markers')
            ->select('users.*')
            ->where('markers.leader_id', '=', Auth::id())
            ->count();

// dd( $count);

        $view->with('count', $count);
    }
}

If I call the results in the front end I get absolutely nothing. If I dd or .toSql() in the compose method, I also get nothing, meaning that the file I have created is not connecting(or sending the variables information) to the view layout.backend somehow.

Any idea where is my error as I have been reading forward and backward the doc without success on this particular problem.

Thank you.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.