如何将laravel数据库查询放入索引的php数组中?

时间:2018-08-13 15:42:55

标签: php sql database laravel

此刻我有这个:

SELECT 
       five9_call_log.agent_name  AS "five9_call_log.agent_name",
       (split_part(five9_call_log.talk_time,':',1)::float *60 )+ 
       split_part(five9_call_log.talk_time,':',2)::float + 
       split_part(five9_call_log.talk_time,':',3)::float / 60  AS 
       "five9_call_log.talk_time_min"
FROM five9_snp.call_log  AS five9_call_log
GROUP BY 1,2
ORDER BY 1 
LIMIT 500

然后在相关的视图文件夹中,它仅显示如下:

$products = Product::whereIn('seller_id', $ids)->orderBy('price','desc')->limit(24)->get(); 

但是,我想将其放置在索引数组中,以便随后为网格布局中显示的产品设置样式。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

$products = Product::whereIn('seller_id', $ids)->orderBy('price','desc')->limit(24)->get(); 
$products->toArray();

答案 1 :(得分:0)

@foreach ($products->chunk(3) as $items)
    <div class="row">
        @foreach ($items as $product)
            <div class="col">
                {{ $product->price }}
            </div>
        @endforeach
    </div>
@endforeach

如果$ products计数为9,则会显示

|price | price | price|
|price | price | price|
|price | price | price|

如果$ products计数为5,则会显示

|price | price | price|
|price | price |

因此,在您的情况下,limit(24)将显示x 8行(每行有3列)