0

I am trying to create a common class for models, where I am passing the model instance to my separate parameter

class Custom
{

    private $model;

    public function getAll()
    {

        return $this->model->get();
    }

    public function  setModel(Model $model)
    {

        $this->model=$model;

    }
}

and my controller I have

$customModel=new Custom();
$customModel->setModel(Branch::class);
echo "<pre>";
print_r($customModel->getAll());

and also I tried

$response=$this->customDatatable->setModel(Branch::class)->getAll();
echo "<pre>";
print_r($response->toArray());

but I got the following Error

Argument 1 passed to App\Helpers\Custom::setModel() must be an instance of Illuminate\Database\Eloquent\Model, string given, called

0

1 Answer 1

3

Branch::class is a class constant that returns the class name as a string, as the error message suggests. Perhaps you're looking to pass new Branch instead.

Sign up to request clarification or add additional context in comments.

2 Comments

@miken.Thanks is there any way i can improve other than passing instance
I don't know, I have no idea what you're trying to accomplish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.