Execute SQL functions with Laravel 5

放肆的年华 提交于 2020-12-26 07:16:51

问题


I have SQL functions stored on my database.

However, I can not call them.

$nb = DB::select('SELECT nb_seances_archivees()');

The result is :

array:1 [▼
   0 => {#186 ▼
      +"nb_seances_archivees": 0
   }
]

But the desired result is just 0.

Thank's for help !


回答1:


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]);


来源:https://stackoverflow.com/questions/47818468/execute-sql-functions-with-laravel-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!