Justin Moh
Justin Moh

Reputation: 1660

How to pass a Model class as parameter so that function can call the class's method?

Tough I'm using this in Laravel 5, but I think it's also a generic php question.

I think the code in index() function is just pretty generic, I probably will be using the same code in just every index() function in every model.

I have them refactored and left one last part here:

public function index() {
    $users = User::paginate($this->paginateItemLimit());
    return $this->successResponder->respondWithPaginator($users);
}

Now how do I make one step further to simply make a call:

public function index() {
    $this->modelIndex(Model);
}

I was thinking perhaps class can be passed by User::class, but how do I utilised it in the function?

Upvotes: 1

Views: 1655

Answers (1)

M0rtiis
M0rtiis

Reputation: 3774

You should create your own basic controller (like BaseController), extend it from Controller and add there a method like

public function modelIndex($model) {
return $this->successResponder->respondWithPaginator(
$model::paginate($this->paginateItemLimit())
);
}

Upvotes: 1

Related Questions