5

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 Answer 1

12

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]);
Sign up to request clarification or add additional context in comments.

9 Comments

And what is DB::table('table') (because functions are not specific at a table)?
Just use: \DB::select(DB::raw('SELECT nb_seances_archivees()'));
With DB::select(DB::raw('SELECT nb_seances_archivees()')); I get the same result and not 0.
Sorry I Thought your query is not working, I updated my answer, take a look
Thank's a lot! What solution do you recommend?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.