php - 如何根据关系对标题进行排序
问题描述
我有以下查询: $scholen = Scholen::with('media', 'translations', 'seo')->online()
我如何从关系->翻译->字段中对“标题”上的学校进行排序
提前致谢。
奥利维尔
解决方案
您可以访问fields
with $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
});
希望这会有所帮助,这看起来有点棘手
推荐阅读
- javascript - 将所有模块导入文件并使用它们
- symfony - FullCalendarBundle 处理 symfony 3.4 中的事件错误
- sql-server - 有什么方法可以在 SQL Server TSQL 中重用 XML 模式约束表达式?
- reactjs - 我无法从 GoogleMap 组件中获取任何节点
- python - 有没有办法使用 VS Code 和 Python 监控 Azure Functions 中的内存消耗?
- angular - 角度测试:无法读取未定义的属性“图像”
- html - 如何将二维数组传递给谷歌图表 html
- awk - 如何根据图案内的条件显示具有开始和结束图案的多线图案
- python - Dandas.DataFrame.loc 的 KeyError
- javascript - 如何将行 ID 从 Flask 传递到 Modal?