Bind a primitive to Laravel IoC container and resolve in a controller method

I'm trying to resolve a primitive inside a controller method.

This is the register method of my Provider:

public function register()
{
    $this->app->when('App\Http\Controllers\InvalidCustomerController')
        ->needs('$customers')
        ->give(function () {
            return InvalidCustomer::latest()
                ->paginate(20);
        });
}

And this is the controller method I'm trying to resolve $customers:

public function index($customers)
{
    return view(
        'customer.invalid.index',
        compact('customers')
    );
}

$customers is not filled.

Everything will work if I resolve that on constructor.

What am I doing wrong?

ps: I'm using Laravel 5.2

Upvotes: 0

Views: 1444

Answers (1)

zuc0001
zuc0001

Reputation: 930

Not sure if you found a solution but to use a primitive in a controller, pass it through the __constructor of the controller class as so:

private $customers;
public function __construct($customers) {
    parent::__construct();
    $this->customers = $customers;
}

This $customers variable can then be used elsewhere inside of the class.

Upvotes: 2

Related Questions