gpt4 book ai didi

php - 从 sql(左外)LARAVEL 以数组形式返回相关数据

转载 作者:行者123 更新时间:2023-11-29 18:23:57 27 4
gpt4 key购买 nike

我查询了此查询,该查询将相关图像返回到评论

  return    $comments = \DB::table('comments')->select('comments.comment','comments.user_name','comments.shop_name',
'comments.rating','comments.created_at','comments_images.images')->
join('comments_images','comments.id','=','comments_images.comment_id','left outer')
->where('comments.product_id', '=', "11644254552")->
orderBy(\DB::raw('-`images`'), 'desc')->get();

它返回这样的结果

{

comment: "fdsfdsfsdffddsffds",
user_name: "ahmaf",
shop_name: "example.com",
rating: "4",
created_at: "2017-09-19 02:14:41",
images: https://example.com/kf/UT8.1.cX3XaXXagOFbX5.jpg
},
-{
comment: "fdsfdsfsdffddsffds",
user_name: "ahmaf",
shop_name: "example.com",
rating: "4",
created_at: "2017-09-19 02:14:41",
images: https://example.com/kf/UT81WzoX3BXXXagOFbXs.jpg
},
-{
comment: "sdfdsfdssfd",
user_name: "fdsfds",
shop_name: "example2.myshopify.com",
rating: "5",
created_at: "2017-09-19 02:12:25",
images: null
}

如您所见,前两行是重复的,我希望它们像这样返回

comment: "fdsfdsfsdffddsffds",
user_name: "ahmaf",
shop_name: "example.com",
rating: "4",
created_at: "2017-09-19 02:14:41",
images:{
0: https://example.com/kf/UT8.1.cX3XaXXagOFbX5.jpg
1: https://example.com/kf/UT81WzoX3BXXXagOFbXs.jpg
}
},

-{
comment: "sdfdsfdssfd",
user_name: "fdsfds",
shop_name: "example2.myshopify.com",
rating: "5",
created_at: "2017-09-19 02:12:25",
images: null
}

这可能吗?返回它们进行收集,而不是为每个相关数据创建新行。

<小时/>

我尝试使用它,但我无法使用 comments_images 表中的 orderBy 图像,它仅适用于评论中的列

$comments = Comment::where('product_id', '=', "11644215052")->with([
'images' => function ($query)
{
$query->orderBy('images', 'desc');
},
])->paginate(30);

最佳答案

您正在寻找父/子关系结果。您不会通过平行结果集得到这一点。看看如何使用 laravel 模型与子关系。

https://laravel.com/docs/5.5/eloquent-relationships#querying-relations

关于php - 从 sql(左外)LARAVEL 以数组形式返回相关数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46347739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com