在laravel雄辩的关系中找到最高价值

时间:2019-11-02 11:58:01

标签: php laravel

因此,我基本上是在尝试查找选定结果的值(可能有所不同),然后将它们进行比较,以根据选定的结果数来找到最高的特定列值。

这是我收集结果的方式:

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
    ]);
}

因此您可以看到,我最终在users[]数组中得到了一个口才的结果,尽管我随后需要从一个称为stats的关系中找到最大值。

我尝试执行以下操作无济于事:

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

抛出以下内容:

  

试图获取非对象的属性“统计”

编辑 我的$user->stats->asArray();函数返回以下内容:

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
            ]
        ]
    ];
}

当尝试从该数组中找到最大值时,我尝试了以下操作:

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

2 个答案:

答案 0 :(得分:1)

由于您要检查$id是否已设置,因此它是可选的,并且foreach块将尝试访问可能为stats的属性null,因此使迭代的条件为很好

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
    ]);
}

您还可以使用optional帮助函数来避免在尝试访问非对象属性时出现错误

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

希望这会有所帮助

答案 1 :(得分:1)

首先我们得到laravel雄辩的关系查询。 例如:

'$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
        ]
    ]

我们可以像这样获得最大价值

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


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

该返回值是laravel雄辩关系中的最高值