Curtis
Curtis

Reputation: 2704

Finding highest value in laravel eloquent relationship

So I'm basically trying to find values of selected results (can vary) and then compare them to find the highest specific column value dependant on how many results are selected.

Here's how I gather my results:

public function index($id = null, $name = null, $id2 = null, $name2 = null, $id3 = null, $name3 = null)
{
    $users = [];
    $jsonStats = [];
    if (isset($id)) {
        $users[] = Users::query()->findOrFail($id);
        if (isset($id2)) {
            $users[] = Users::query()->findOrFail($id2);
            if (isset($id3)) {
                $users[] = Users::query()->findOrFail($id3);
            }
        }
    }
    foreach ($users as $user) {
        $jsonStats[] = $user->stats->asArray();
    }
    return view('frontend.index', [
        'users' => $users,
        'stats_json' => $jsonStats
    ]);
}

So as you can see I finally get an Eloquent result within the users[] array, although I then need to find the highest value from within a relationship called stats

I've attempted to do the following to no avail:

{{ max(array_column($users->stats, 'stat1')) }}

Which throws the following:

Trying to get property 'stats' of non-object

EDIT My $user->stats->asArray(); function returns the following:

public function asArray()
{
    return [
        'stat1' => [
            [
                'id' => 'attribute1',
                'name' => 'Attribute1',
                'type' => 'main',
                'value' => $this->attr1Value
            ],
            [
                'id' => 'attribute2',
                'name' => 'Attribute2',
                'type' => 'main',
                'value' => $this->attr2Value
            ],
            [
                'id' => 'attribute3',
                'name' => 'Attribute3',
                'type' => 'main',
                'value' => $this->attr3Value
            ]
        ]
    ];
}

When trying to find the max value from this array, I've tried the following:

{{ max(array_column($stats_json['stat1'][0], 'value')) }}

Upvotes: 2

Views: 1187

Answers (2)

Gopinath Sooriyakumar
Gopinath Sooriyakumar

Reputation: 76

First We get laravel eloquent relationship query. Ex:

'$data' => [
        [
            'id' => '1',
            'name' => 'subject1',
            'type' => 'T',
            'value' => $this->Value
        ],
        [
            'id' => '2',
            'name' => 'subject2',
            'type' => 'M',
            'value' => $this->Value
        ],
        [
            'id' => '3',
            'name' => 'subject2',
            'type' => 'T',
            'value' => $this->Value
        ]
    ]

We can get max value someting like this

$max1 = max(array_column($data, 'value')) // only get value max


foreach ($data as $array) {
     if (in_array($array['value'],$max1)) {
         return $array;
     }
} 

That return value is the highest value in laravel eloquent relationship

Upvotes: 2

Salim Djerbouh
Salim Djerbouh

Reputation: 11034

Since you're checking if the $id is set then it's optional and the foreach block will try to access a property stats of what could be null, make the iteration conditional as well

public function index($id = null, $name = null, $id2 = null, $name2 = null, $id3 = null, $name3 = null)
{
    $users = [];
    $jsonStats = [];
    if (isset($id)) {
        $users[] = Users::query()->findOrFail($id);
        if (isset($id2)) {
            $users[] = Users::query()->findOrFail($id2);
            if (isset($id3)) {
                $users[] = Users::query()->findOrFail($id3);
            }
        }
        foreach ($users as $user) {
            $jsonStats[] = $user->stats->asArray();
        }
    }
    return view('frontend.index', [
        'users' => $users,
        'stats_json' => $jsonStats
    ]);
}

You could also use the optional helper function to avoid errors upon trying to access properties of non objects

$jsonStats[] = optional($user)->stats->asArray();

Hope this helps

Upvotes: 1

Related Questions