You can do this using laravel eloquant. First create these modals
Country, PaymentMethod, CountryPaymentMethod, PaymentMethodConfiguration
And then create these relationships inside modals.
// Country.php
public function countryPaymentMethods()
{
return $this->hasMany(CountryPaymentMethod::class);
}
// PaymentMethod.php
public function countryPaymentMethods()
{
return $this->hasMany(CountryPaymentMethod::class);
}
// CountryPaymentMethod.php
public function configurations()
{
return $this->hasMany(PaymentMethodConfiguration::class);
}
public function country()
{
return $this->belongsTo(Country::class);
}
public function paymentMethod()
{
return $this->belongsTo(PaymentMethod::class);
}
// PaymentMethodConfiguration.php
public function countryPaymentMethod()
{
return $this->belongsTo(CountryPaymentMethod::class);
}
And you can query them like this
$country = Country::where('name', 'Ireland')->first();
$paymentMethod = PaymentMethod::where('name', 'Mobile Money')->first();
$configurations = PaymentMethodConfiguration::whereHas('countryPaymentMethod', function ($query) use ($country, $paymentMethod) {
$query->where('country_id', $country->id)
->where('payment_method_id', $paymentMethod->id)
->where('is_active', true);
})->get();
Study about this more here
Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean?