O
O
ObehanProger2019-02-15 10:41:58
Laravel
ObehanProger, 2019-02-15 10:41:58

How to pass a parameter through a service provider to the constructor of an implementation class?

I did not find in the Laravel documentation how to pass a variable from the TestController controller through the service provider to the constructor of the PostRepository implementation class that selects posts from the database. I give only the most significant
PostRepository code:

public $city;
      public function __construct($city){
        $posts = DB::table('post')->where('city',  $city)->get();     
        $this->posts=$posts;
      }

Post Service Provider:
public function register(){
       $this->app->singleton('App\Repositories\PostRepository', function(){
        return $posts;
      });
    }

TestController:
public function test(){
      $city='1';
       var_dump(app('App\Repositories\PostRepository'));
      }

Mistake:
Unresolvable dependency resolving [$parameter] in class {$parameter->getDeclaringClass()->getName()}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question