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

时间:2014-10-01 20:10:35

标签: php mysql arrays laravel laravel-4

我正在使用Laravel 4.2开发一个项目,我有一行从一个名为Gifts

的表中检索一些行
$theGifts = Gift::whereIn('id', $giftImages)->get();

此变量$giftImages是一个数组,值按此顺序排序

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

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

我如何确保订单相同?我正在使用控制器中对象的数据,而不是顺便提供的视图。

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
                )

        )

)

1 个答案:

答案 0 :(得分:1)

我相信这会为你做到:

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

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

相关问题