More Eloquent methods

September 16, 2024

Finally made use of Laravel's withWhereHas method

We're on a stretch here recently of using Eloquent methods I don't often get to. In this case, this was my first time reaching for the withWhereHas method. The story goes, I needed to find Group Member Payments but only those that had a specific Payment type. The model relationship here is a Payment is made up of many Group Member Payments and payment_type lives on the Payment model.

GroupMemberPayment::withWhereHas("payment", function ($query) {
	$query->where("payment_type", Payment::TYPE_TRANSFER);
})->get();

It was kind of amazing to see how easy this method make it to retrieve only models where their related data meets a specific condition.


© 2024 Terrence Eisenhower. All rights reserved.