I have a hasOne relationship. The image shows the records of the relationship which have the same "contract_id"
Following result is required:
I want to only select the last record with the highest "version" => 5, but only if the "status" column is not "draft". Otherwise don’t select the parent record at all if the relationship doesn’t fulfill the condition. ($contract::with(‘latestVersion’)->paginate())
I don’t want to get the next possible record that fullfills the condition – meaning:
I don’t want to get "version 3" with "status active"
I tried to sort by latest() or groupBy(‘version’)…
2
Answers
add
->where('status', '!=', 'draft')
to your conditionsmore on the topic: https://laravel.com/docs/10.x/queries#where-clauses
Maybe you can use
whereHas
orwithWhereHas
that put additionalwhere
conditions on yourhas
queries.Reference: https://laravel.com/docs/10.x/eloquent-relationships#querying-relationship-existence
Hope this will be helpful