I have a repo and a eloquentrepo, see code. Now I want to pass a var into the constructor via the app->make()
Is that possible and how can I achieve that?
AppServiceProvoder:
app()->bind(TestRepository::class, EloquentTestRepository::class);
Appmake:
app()->(TestRepository::class)->getItem(TestModel::TEST_ITEM);
class EloquentTestRepository implements TestRepository {
__construct($var){
// do something with the $var
}
}
Try:
app()->(TestRepository::class,['test'])->getItem(TestModel::TEST_ITEM);
public function __construct( $test)
{
echo $test;
}
Error:
Illuminate\Contracts\Container\BindingResolutionException: Unresolvable dependency resolving [Parameter #0 [ <required> $test ]]
Solution: In the AppServiceProvider:
app()->bind(FrequentieRepository::class,
function($test){
return new EloquentTestRepository($test);
});
The second parameter can be an array of arguments, check it here .
Illuminate/Foundation/Application
/**
* Resolve the given type from the container.
*
* (Overriding Container::make)
*
* @param string $abstract
* @param array $parameters
* @return mixed
*/
public function make($abstract, array $parameters = [])
{
$abstract = $this->getAlias($abstract);
if (isset($this->deferredServices[$abstract]) && ! isset($this->instances[$abstract])) {
$this->loadDeferredProvider($abstract);
}
return parent::make($abstract, $parameters);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.