Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gadreel's avatar

ofMany based on a relation

I have a model "Lead" this model has an one to many relationship with a model "ActivityLead" and there is an one to one polymorphic table with "ActivityDetail" which contain extra information about the activity.

The "ActivityDetail" has these 2 columns "proposed_start_at" which is a date and a boolean column "is_completed".

I want with Laravel's ofMany method to get the upcoming incomplete activity in which I need to sort the proposed_start_at MIN and check if is_complete is false.

But how to do that since these information are on the 3rd model "ActivityDetail"?

The below wont work because "proposed_start_at" is not on the ActivityLead model.

Thanks.

/**
     * Get the upcoming incomplete activity.
     *
     * @return HasOne
     */
    public function upcomingIncompleteActivity(): HasOne
    {
        return $this->hasOne(ActivityLead::class)->ofMany([
            'proposed_start_at' => 'min'
        ], function ($query) {
            $query->whereHas('details', function (Builder $query) {
                $query->where('is_completed', false);
            });
        });
    }
0 likes
0 replies

Please or to participate in this conversation.