Answer the question
In order to leave comments, you need to log in
How to implement DI?
there is an index method in it, I use a third-party class (package) Resolver that accepts Eloquent\Builder . I want to make EloquentResolver through DI.
public function index($argument)
{
$resolver = new Resolver(Model::query);
$result = $resolver->build($argument);
return $result;
}
Answer the question
In order to leave comments, you need to log in
DI is not a panacea, therefore:
Resolver.php
public function via(Builder $builder)
{
$this->builder = $builder;
}
public function build($argument)
{
$this->builder->where('field', $argument)->...;
}
$this->singletons = [
Resolver::class => Resolver::class
];
public function __construct(Resolver $resolver)
{
$this->resolver = $resolver;
}
public function index($argument)
{
return $resolver->via(Model::query())->build($argument);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question