topclaudy / compoships

Multi-columns relationships for Laravel's Eloquent ORM
MIT License
1.1k stars 130 forks source link

Adding function to support the new ofMany() function #126

Closed ryross closed 2 years ago

ryross commented 2 years ago

This PR adds support for ofMany(). See https://laravel-news.com/one-of-many-eloquent-relationship

public function latestAgentContact()
{
    return $this->hasOne(Contact::class, ['clientid', 'agentid'], ['id', 'agentid'])->ofMany(['contactdate' => 'max']);
}
topclaudy commented 2 years ago

Thanks @ryross. Can you please add test(s)?

erikn69 commented 2 years ago

For avoid many commits you can squash them

git reset --soft HEAD~
git reset --soft HEAD~
git reset --soft HEAD~
git commit --verbose --amend -C HEAD
git push origin -f

git reset --soft HEAD~ uncommit git commit --verbose --amend -C HEAD add to the last commit git push origin -f force push with new commit

erikn69 commented 2 years ago

Maybe a global function could help, like this

bootstrap.php

if (! function_exists('getLaravelMayorVersion')) {
    function getLaravelMayorVersion () {
        $version = explode('.', app()->version());

        return (int)$version[0];
    }
}

Allocation.php

public function smallerTrackingTask()
{
    $rel = $this->hasOne(TrackingTask::class, ['booking_id', 'vehicle_id'], ['booking_id', 'vehicle_id'])-

    return getLaravelMayorVersion() < 8 ? $rel :  $rel->ofMany('id', 'min');
}
public function latestTrackingTask()
{
    $rel = $this->hasOne(TrackingTask::class, ['booking_id', 'vehicle_id'], ['booking_id', 'vehicle_id']);

    return getLaravelMayorVersion() < 8 ? $rel :  $rel->latestOfMany();
}
public function oldestTrackingTask()
{
    $rel = $this->hasOne(TrackingTask::class, ['booking_id', 'vehicle_id'], ['booking_id', 'vehicle_id']);

    return getLaravelMayorVersion() < 8 ? $rel :  $rel->oldestOfMany();
}

HasManyTest

public function test_Compoships_hasOneOrMany_saveMany()
{
    ////**

    if(getLaravelMayorVersion() >= 8) {
        $this->assertEquals($expectedData[0], $allocation->smallerTrackingTask->toArray());
        $this->assertEquals($expectedData[0], $allocation->oldestTrackingTask->toArray());
        $this->assertEquals($expectedData[2], $allocation->latestTrackingTask->toArray());
    }

    ////**
}
topclaudy commented 2 years ago

I merged the implementation with tests https://github.com/topclaudy/compoships/pull/128