0
votes

config/app.php

'providers' => [
     // ...
     App\Providers\ViewServiceProvider::class,
     App\Http\View\Composers\AdComposer::class,
],

app/Providers/ViewServiceProvider.php

<?php

namespace App\Providers;

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

class ViewServiceProvider extends ServiceProvider
{

    public function register()
    {
        //
    }

    public function boot()
    {
        View::composer(
            'layouts.sidebar', 'App\Http\View\Composers\AdComposer'
        );
    }
}

app/Http/View/Composers/AdComposer.php

<?php

namespace App\Http\View\Composers;

use App\Ad;
use Illuminate\View\View;
use Illuminate\Support\Facades\Request;

class AdComposer
{

    protected $ads;

    public function __construct(Ad $ads)
    {
        $this->ads = $ads;
    }

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

my problem is:

TypeError Argument 1 passed to App\Http\View\Composers\AdComposer::__construct() must be an instance of App\Ad, instance of Illuminate\Foundation\Application given, called in E:\appoo\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php on line 208

How to resovle this problem and pass $ads to view?

1

1 Answers

1
votes

Remove the AdComposer from the providers array.

The docs state:

Remember, if you create a new service provider to contain your view composer registrations, you will need to add the service provider to the providers array in the config/app.php configuration file.

It does not tell you to add your composer to the providers array.

Also, if you type-hint App\Ad you will get an empty instance just so you're aware.