Support the ongoing development of Laravel.io →
Eloquent Architecture

How could I do to get access to the all(), find(), and all Model static functions if I'm using an instance of the Model injected to my BaseRepository?

class BaseRepository {

       protected $model;

       public function __construct(ModelName $model){

                   $this->model = $model;
       }

       public function getAll(){

                   //how could I use $model::all();
       }
}

Here ModelName is a name of a Model.

Thanks in advanced.

Last updated 3 years ago.
0

You can use $this->model->all()

Last updated 3 years ago.
0

TorchSK said:

You can use $this->model->all()

Many thanks TorchSK,

I haven't access to this static method.

Last updated 3 years ago.
0

And I think that this part

public function __construct(ModelName $model){
    $this->model = $model;
}

belongs to the repository that extends BaseRepository

Last updated 3 years ago.
0

TorchSK said:

And I think that this part

public function __construct(ModelName $model){ $this->model = $model; }

belongs to the repository that extends BaseRepository

Many thanks TorchSK i will try it.

Last updated 3 years ago.
0
   abstract class BaseRepository {

          protected $model;

          public function __construct($model = null)
          {
                 $this->model = $model;
          }

          public function getAll()
          {
                 $this->model->all();
          }

          public function findById($id)
          {
                 $this->model->find($id);
          }
   }
Last updated 3 years ago.
0

Many thanks for all both!

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.