Execute SQL functions with Laravel 5

前端 未结 1 462
一个人的身影
一个人的身影 2021-01-19 14:52

I have SQL functions stored on my database.

However, I can not call them.

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

The

相关标签:
1条回答
  • 2021-01-19 15:08

    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]);
    
    0 讨论(0)
提交回复
热议问题