Construct values causing unresolvable primitive exception when using WPSober Controller

Trying to add a constructor function to one my controllers. However, I am getting this error before I’ve even had a chance to instantiate the class in the my blade file.

Uncaught Exception: Unresolvable dependency resolving [Parameter #0 [ <required> string $post_id ]] in class App\Controllers\Translate

Here’s my construct:

function __construct(string $post_id, string $target_language)
    $this->post_id = $post_id;
    $this->target_language = $target_language;

Here’s the exception from Container.php:

     * Throw an exception for an unresolvable primitive.
     * @param  \ReflectionParameter  $parameter
     * @return void
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
    protected function unresolvablePrimitive(ReflectionParameter $parameter)
        $message = "Unresolvable dependency resolving [$parameter] in class {$parameter->getDeclaringClass()->getName()}";

        throw new BindingResolutionException($message);

Any idea what could be causing this? Are we not suppose to add constructors in controllers?

Note: I am using v. 2.1.0. I didn’t see any older issues that would make me think upgrading would resolve this.

Hi @rguttersohn,
not sure if it has any effect - but is $post_id in the constructor really a string? (int?)

I am pretty sure you are right. This was my attempt to figure out where the error was occurring. This is how I knew it wasn’t coming from the instantiation of the class in the blade template. And when I delete the constructor, the error goes away.

This topic was automatically closed after 42 days. New replies are no longer allowed.