0

I trouble to pass eloquent methods as a parameter.

Here is my method,

public function getPayment($paymentId, $eloquentMethod)
{
    if (!empty($orderId)) {
        return DB::connection('my_db')->table('payment')->{$eloquentMethod}();
    }
    return null;
}

It is working properly if i pass $this->getPayment('1', 'get')

But it is not working if i pass $this->getPayment('1', 'pluck("payment_id")')

Thanks in advance.

4
  • Did you mean $eloquentMethod = 'get'? Commented Nov 14, 2017 at 11:50
  • Try: $eloquentMethod = 'get' and then use return DB::connection('my_db')->table('payment')->{$eloquentMethod}(); Commented Nov 14, 2017 at 11:52
  • @tyteen4a03 yes but with parameter. Commented Nov 14, 2017 at 12:10
  • @HirenGohel yes but how to pass parameter like : pluck('id') Commented Nov 14, 2017 at 12:11

1 Answer 1

1

How much possibilities do you have for $eloquentMethod ? If many possibilities such as "get","first"..etc then you can make:

public function getPayment($paymentId, $eloquentMethod = 'get()')
{
    if (!empty($orderId)) {
        switch ($eloquentMethod) {
            case "get":
                return DB::connection('my_db')->table('payment')->get();
                break;
            case "first":
                return DB::connection('my_db')->table('payment')->first();
                break;
            case default:
                return null;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.