0

so i have followed many tutorials and videos and checked here and cant seem to get this to work, really need help. im going to provide my code and explain what im trying to so and hopefully someone can help me sort this out. so what im trying to so is to pass my $user and $company data from my database to my partial layout though view composer. i used php artisan to make a composerserviceprovider and and dashboardcomposer in my view/composer folder and i get errors.

my composerserviceprovider.php is as follows

    <?php

namespace App\Providers;


use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use App\companies;
use App\user;

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

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer("layouts.dashboard","App\View\Composers\dashboardnavComposer");
    }

}

my dashboardnavcomposer.php is as follows

<?



namespace App\View\Composers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use App\companies;
use App\user;


class dashboardnavComposer
{
    public function compose(View $view)
    {
        $view->with('user', ComposerServiceProvider::all());

    }
}

im probably doing something completely wrong or completely missing a step, i already did the php artisan dumpautoload, so this is not the problem. any help would be appreciated. also to let you know, i use to do this 10+ years ago and i quit to be a truck driver and so last time i did this php4 was king. so please understand i just starting relearning this about 30 days ago.

thanks in advance for any help you can provide.

Anthony Moore
  • 84
  • 1
  • 9
  • the error i get is Target class [App\View\Composers\dashboardnavComposer] does not exist. (View: /Users/mymacbookpro/nginxSites/backoffice/resources/views/backoffice/homepage.blade.php) – Anthony Moore Dec 07 '19 at 03:32

1 Answers1

0

Not sure what you filename actually is for the composer but your filename should match the class name (exactly in case). The file should be named dashboardnavComposer.php. In reality the class should probably be more like DashboardNavComposer and the file would be DashboardNavComposer.php. Also make sure it is in the app/Views/Composers folder.

I am not sure why your View Composer is calling to a Service Provider, ComposerServiceProvider, though.

The PSR-4 autoloading should be picking up these new files. You can run composer dump just in case.

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • I'm not sure either I've forgotten so much I'm not even sure I've done anything right – Anthony Moore Dec 07 '19 at 17:30
  • i changed the file name and the class name and then that didnt work so i changed the function to this: ` public function compose(View $view) { $user = Auth::user(); $company = companies::findOrFail($user->companyID); $view->with(compact('company', $company)) ->with(compact('user', $user)); }` – Anthony Moore Dec 07 '19 at 17:55
  • i changed the file name and the class name and then that didnt work so i changed the function to this: `code` public function compose(View $view) { $user = Auth::user(); $company = companies::findOrFail($user->companyID); $view->with(compact('company', $company)) ->with(compact('user', $user)); }`code` i still get the same error that the dashboardnavcomposer does not exist – Anthony Moore Dec 07 '19 at 18:01
  • that isn't how compact works btw, check the manual for how to use compact, or just dont use it and create the array yourself – lagbox Dec 07 '19 at 23:04
  • ok so i fixed that, i was just trying to copy someone elses from another post that got it to work and i just tried that to see if it would work, however i am still getting the error, but i have i still cant figure out why i get the error that my target dashboardcomposer does not exist, i have checked and checked everything is spelled right and im just at a loss here. please someone help or just put me out of my misery lol. – Anthony Moore Dec 08 '19 at 01:11
  • also i changed to public function boot() { View::composer('*', 'app\view\composer\DashboardComposer'); } in my composerserviceprovider, also i have in fact added my service providers to my app.php serviceproviders list. – Anthony Moore Dec 08 '19 at 01:14
  • you have to be correct with the case of letters ... `app\view\composer\...` isn't `App\View\Composers\...` – lagbox Dec 08 '19 at 01:52
  • ok now i moved the file to ViewComposers and updated my namespace and my use to match: namespace app\ViewComposers; its now use app\ViewComposers; its now View::composer('layouts.dashboard', 'app\ViewComposers\DashboardComposer'); the class name is now DashboardComposer and so is the file name plus .php and the other is still ComposerServiceProvider.php after these changes i still get Facade\Ignition\Exceptions\ViewException Target class [app\ViewComposers\DashboardComposer] does not exist. – Anthony Moore Dec 08 '19 at 10:01
  • its `App` not `app`, this is part of the PSR4 autoloading via composer, look at your composer.json file to see that part ... this is why you can add classes in these folders with namespaces and they are automatically found without dumping the autoloader – lagbox Dec 08 '19 at 10:09
  • ok i changed that, yet its still not finding the DashboardComposer, same error – Anthony Moore Dec 08 '19 at 13:40