首页 > 解决方案 > 如何根据关系对标题进行排序

问题描述

我有以下查询: $scholen = Scholen::with('media', 'translations', 'seo')->online()

当我倾倒它时: 在此处输入图像描述

我如何从关系->翻译->字段中对“标题”上的学校进行排序

提前致谢。

奥利维尔

标签: phplaravel

解决方案


您可以访问fieldswith $scholen[$index]->translations[$index]->fields,但是您无法访问 title 属性,因为 的整个值fields是一个字符串 - 您必须剥离字符串以获得您想要的位。

您将不得不get()查询结果,然后通过在sortBy()回调中包含此逻辑对结果进行排序,例如

$scholen->sortBy(function ($value, $key) {
    // Logic here to get the part of the `fields` string you want
    // and then sort it
});

希望这会有所帮助,这看起来有点棘手


推荐阅读