gpt4 book ai didi

php - 如何保持收集的数据库行的顺序与传递给 where 子句的数组相同

转载 作者:行者123 更新时间:2023-11-29 12:40:51 25 4
gpt4 key购买 nike

我正在使用 Laravel 4.2 开发一个项目,并且我有一行从名为 Gifts 的表中检索一些行

$theGifts = Gift::whereIn('id', $giftImages)->get();

这个变量$giftImages是一个数组,值的排序如下

Array 
(
[0] => 5
[1] => 2
[2] => 3
[3] => 4
)

当我输出这个$theGifts时,检索到的结果与 ids 数组的顺序不同,我需要它的顺序相同..

如何确保顺序相同?我使用的是 Controller 中对象的数据,而不是 View 。

Gift::all() 的打印输出如下所示(以防有人需要比较)..

Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 2
[name] => Banana
[image] => bananas-icon.png
[value] => 10
)

[original:protected] => Array
(
[id] => 2
[name] => Banana
[image] => bananas-icon.png
[value] => 10
)

[relations:protected] => Array
(
)

[hidden:protected] => Array
(
)

[visible:protected] => Array
(
)

[appends:protected] => Array
(
)

[fillable:protected] => Array
(
)

[guarded:protected] => Array
(
[0] => *
)

[dates:protected] => Array
(
)

[touches:protected] => Array
(
)

[observables:protected] => Array
(
)

[with:protected] => Array
(
)

[morphClass:protected] =>
[exists] => 1
)

[1] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 3
[name] => Cherry Cake
[image] => Cherry-Cake-icon.png
[value] => 15
)

[original:protected] => Array
(
[id] => 3
[name] => Cherry Cake
[image] => Cherry-Cake-icon.png
[value] => 15
)

[relations:protected] => Array
(
)

[hidden:protected] => Array
(
)

[visible:protected] => Array
(
)

[appends:protected] => Array
(
)

[fillable:protected] => Array
(
)

[guarded:protected] => Array
(
[0] => *
)

[dates:protected] => Array
(
)

[touches:protected] => Array
(
)

[observables:protected] => Array
(
)

[with:protected] => Array
(
)

[morphClass:protected] =>
[exists] => 1
)

[2] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 4
[name] => Coconut
[image] => coconut-icon.png
[value] => 5
)

[original:protected] => Array
(
[id] => 4
[name] => Coconut
[image] => coconut-icon.png
[value] => 5
)

[relations:protected] => Array
(
)

[hidden:protected] => Array
(
)

[visible:protected] => Array
(
)

[appends:protected] => Array
(
)

[fillable:protected] => Array
(
)

[guarded:protected] => Array
(
[0] => *
)

[dates:protected] => Array
(
)

[touches:protected] => Array
(
)

[observables:protected] => Array
(
)

[with:protected] => Array
(
)

[morphClass:protected] =>
[exists] => 1
)

[3] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 5
[name] => Lemon
[image] => lemon-icon.png
[value] => 3
)

[original:protected] => Array
(
[id] => 5
[name] => Lemon
[image] => lemon-icon.png
[value] => 3
)

[relations:protected] => Array
(
)

[hidden:protected] => Array
(
)

[visible:protected] => Array
(
)

[appends:protected] => Array
(
)

[fillable:protected] => Array
(
)

[guarded:protected] => Array
(
[0] => *
)

[dates:protected] => Array
(
)

[touches:protected] => Array
(
)

[observables:protected] => Array
(
)

[with:protected] => Array
(
)

[morphClass:protected] =>
[exists] => 1
)

)

)

最佳答案

我相信这会适合你:

Gift::whereIn('id', $giftImages)
->orderBy(DB::raw('FIELD(id, ' . implode(',', $giftImages) . ')'), 'ASC')
->get();

取自 this question 。您必须使用 DB::raw() 因为 Laravel 不提供本地方法来执行此操作。未经测试...

关于php - 如何保持收集的数据库行的顺序与传递给 where 子句的数组相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26149737/

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