使用Laravel 5执行SQL函数 [英] Execute SQL functions with Laravel 5
本文介绍了使用Laravel 5执行SQL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库中存储有SQL函数.
I have SQL functions stored on my database.
但是,我不能给他们打电话.
However, I can not call them.
$nb = DB::select('SELECT nb_seances_archivees()');
结果是:
array:1 [▼
0 => {#186 ▼
+"nb_seances_archivees": 0
}
]
但是所需的结果只是 0
.
谢谢您的帮助!
推荐答案
默认情况下, DB :: select
返回对象数组,可以使用
By default DB::select
return an array of objects, you can use collections to get the first result:
$nb = collect(DB::select('SELECT nb_seances_archivees() AS nb'))->first()->nb;
或直接访问数组中的第一个对象:
Or directly access the first object in the array:
$nb = DB::select('SELECT nb_seances_archivees() AS nb')[0]->nb;
如果要传递参数,则应该这样做:
If you want to pass parameters then you should do:
DB::select('SELECT nb_seances_archivees(?) AS nb', [$parameter]);
这篇关于使用Laravel 5执行SQL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文