samedi 8 juin 2019

Accept two different model instances as a function argument php

I'm writing a helper function to check whether any address information is present in my database by checking if any of the relevant fields don't equal null.

So, I have two models which I need to accept in the function as an argument.

  • App\Customer
  • App\Supplier

The variable passed can be either one.

For a function that should only accept one kind of model, I can just do this:

function check_for_address_info(App\Customer $customer) {
    // other unrelated stuff
}

Is there a way to accept both models, or do I have to manually check it in the function by doing something like this:

function check_for_address_info($param) {
  if(!is_a($param, 'App\Customer' || !is_a($param, 'App\Supplier')) {
    // not an instance of either model
    return false;
  }

  // do stuff as normal
}

Any ideas on how to accept two different models as a function argument?

I'm on Laravel 5.8.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2wJNhs8
via IFTTT

Aucun commentaire:

Enregistrer un commentaire