Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

clat23's avatar

Unable to get Spark Notifications to work...

I am following this documentation: https://spark.laravel.com/docs/4.0/notifications

ResponseErrorHandler.php

<?php

namespace App;

use Laravel\Spark\Contracts\Repositories\NotificationRepository;

class ResponseErrorHandler
{
    public function __construct(NotificationRepository $notifications)
    {
      $this->notifications = $notifications;
    }
    
    public function __get($error)
    {
        if (method_exists($this, $error)) {
            return $this->{$error}();
        } 
    }

    public function error_931()
    {
        $this->notifications->create($user, [
            'icon' => 'fa-users',
            'body' => 'Test',
            'action_text' => 'Test Button',
            'action_url' => '/test',
        ]);
    }
}

MyCustomController.php

<?php

namespace App\Http\Controllers;

use App\ResponseErrorHandler;

class MyCustomController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
    // Triggering with hard coded error to test
        (new ResponseErrorHandler)->error_931;
     }
}

When I visit the route that fires the index method in my controller I get the following FatalThrowableError:

Type error: Too few arguments to function App\ResponseErrorHandler::__construct(), 0 passed

How do I fix this? Any help is appreciated!

0 likes
6 replies
tekmi's avatar
tekmi
Best Answer
Level 23

@clat23

Your class ResponseErrorHandler expects first parameter to be of type NotificationRepository

If NotificationRepository is a class without any dependencies, then you could fix it like

(new ResponseErrorHandler(new NotificationRepository()))->error_931();

However Spark documentations says

To create a notification, inject the Laravel\Spark\Contracts\Repositories\NotificationRepository into your controller or any other class which is being resolved by the Laravel service container

I doubt your class ResponseErrorHandler can be resolved by Laravel's service container - make sure you register it in your Service Provider - in this case you won't need to instantiate it by using new ResponseErrorHandler like you were doing.

clat23's avatar

Thank you @tekmi I've created a service provider for ResponseErrorHandler by running:

php artisan make:provider ResponseErrorHandlerServiceProvider

Here's the ResponseErrorHandlerServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ResponseErrorHandlerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('App\ResponseErrorHandler', function ($app) {
            return new ResponseErrorHandler;
        });
    }
}

I then added App\Providers\ResponseErrorHandlerServiceProvider::class, to config\app.php

I still get the same error:

Type error: Too few arguments to function App\ResponseErrorHandler::__construct(), 0 passed

I noticed you mentioned:

in this case you won't need to instantiate it by using new ResponseErrorHandler like you were doing.

I am still instantiating in that way. Once it's been registered as a service provider, how would I now instantiate it?

Also, do I have the correct binding in the register method of my service provider?

I'm sorry for the many questions, this is my first time writing a service provider and registering it. Thank you for your help!

tekmi's avatar

@clat23 you are on the right track.

Have you seen those episodes about Service Container?

https://laracasts.com/series/laravel-from-scratch-2017/episodes/24 https://laracasts.com/series/laravel-from-scratch-2017/episodes/25

Those should help you understand it better. When it comes to resolving classes out of Service Container, it's also nice to study this part of the Laravel docs https://laravel.com/docs/5.4/container#resolving

Check if you are already able to resolve your class with all dependencies by running

dd(App::make('App\ResponseErrorHandler'));

If not, you may need to do some adjustment in your ResponseErrorHandlerServiceProvider:

$this->app->bind('App\ResponseErrorHandler', function ($app) {
    return new ResponseErrorHandler(new NotificationRepository());
});

or maybe like this

$this->app->bind('App\ResponseErrorHandler', function ($app) {
    return new ResponseErrorHandler($app->make(NotificationRepository::class));
});
clat23's avatar

@tekmi Thank you and yes I watched those episodes when I was starting out with Laravel. Back then, it didn't make sense because I was so new. I watched it again today and it made more sense. So thank you for your guidance.

I made a few adjustments based on those videos and I now get this when I run dd(App::make('App\ResponseErrorHandler'));:

ResponseErrorHandler {#254 ▼
  +"notifications": NotificationRepository {#256}
}

But I still have a problem. When I run (new ResponseErrorHandler)->error_931;, I still get:

Type error: Too few arguments to function App\ResponseErrorHandler::__construct(), 0 passed

So then I tried changing it to run this (new ResponseErrorHandler(new NotificationRepository()))->error_931();, but I get this error:

[Symfony\Component\Debug\Exception\FatalThrowableError] Class 'App\NotificationRepository' not found

So I tried fixing it by adding use Laravel\Spark\Contracts\Repositories\NotificationRepository;, but then I got this error:

[Symfony\Component\Debug\Exception\FatalThrowableError] Cannot instantiate interface Laravel\Spark\Contracts\Repositories\NotificationRepository

I'm thinking that I am not instantiating it correctly now that I have registered ErrorHandlerServiceProvider?

I am using the magic method __get as you recommended to me here: https://laracasts.com/discuss/channels/laravel/handling-errors-from-an-outside-api

On your first reply to me in this discussion you said:

I doubt your class ResponseErrorHandler can be resolved by Laravel's service container - make sure you register it in your Service Provider - in this case you won't need to instantiate it by using new ResponseErrorHandler like you were doing.

How would I now instantiate it?

...Thank you again for being so helpful!

clat23's avatar

Nevermind, I figured it out. I instantiated it like this:

$handle = \App::make('App\ResponseErrorHandler');
$handle->error_931;

Thank you, thank you, thank @tekmi !!!

Please or to participate in this conversation.