Lecturize / Laravel-Addresses

Simple address and contact management for Laravel.
MIT License
197 stars 39 forks source link

Add fallback logic to Address and Contact flags #39

Closed bfiessinger closed 1 year ago

bfiessinger commented 1 year ago

implements flag fallbacks for Addresses as described in #32


Currently no flag fallback support

By using the current getters like $model->getBillingAddress() the return is way too strict and in many cases we will get a null return. But there might be a primary / shipping / any other address attached to the model that could also be used by the application.

In theses cases there should be fallback.

The solution

Besides the existing methods like getPrimaryAddress, getBillingAddress, etc. I have added a simple getAddress method. This method takes 3 arguments:

getAddress will first check if addresses are existing on the model and if so returns the first / last address depending on the $direction param.

If no address is found to this point it will create a fallback order based on config('lecturize.addresses.flags') and returns the next best result. If no address matches any result in this order we fallback to either the first or last address with any or no flag.

Contacts do not have any methods to query by flag

While we could write a query ourselfes like:

$model->contacts()
    ->where('is_primary', 1)
    ...

it would be much more convenient to use the same methods known from the Addresses.

So contacts can be now also be queried by $model->getContact($flag, $direction, $strict)