【问题标题】:Finding highest value in laravel eloquent relationship在 laravel 雄辩的关系中寻找最高价值
【发布时间】:2019-11-02 11:58:01
【问题描述】:

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

这是我收集结果的方式:

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[] 数组中得到了 Eloquent 结果,尽管我需要从名为 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')) }}

【问题讨论】:

  • 如果$user 不是一个对象而不是它可能为空,尝试在if(isset($id)) { 下记录一些info($id); 以验证if 块没有完全跳过
  • @Saly3301 用户将始终根据访问方式获得结果,此错误也来自设置 2 users
  • 不,如果没有设置 id,if 语句将被跳过,$users 将是一个空数组,意味着你将迭代空值
  • 另外你不能访问数组的属性$users->stats
  • @Saly3301 好的,那么我如何找到最大值?

标签: php laravel


【解决方案1】:

首先我们得到 laravel eloquent 关系查询。 例如:

'$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 eloquent关系中的最高值

【讨论】:

    【解决方案2】:

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

    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();
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 2015-03-14
      • 2018-11-28
      • 2015-01-11
      • 2021-06-03
      相关资源
      最近更新 更多