Laravel-5 returns an eloquent object when starting a MySQL stored procedure

I would like to know if the MySQL stored procedure call can be returned as an eloquent object.

The following call works fine for me, but $result always returns an array instead of the usual Eloquent object.

$result = DB::select('call bookings_by_voucher()');

Does anyone know how to return this as an object so that I can use ->count() , ->get() , etc.

+5
source share
3 answers

You must pass the array to a new instance of your eloquent object.

 $booking = new Booking($result); 
+1
source

Late question, but if anyone else needs it:

 Booking::fromQuery('call bookings_by_voucher()'); 

The only problem is that you cannot perform further sql operations, such as where, limit, etc., since you cannot directly manipulate the stored procedure, for example: you cannot execute "call bookings_by_voucher (), where active = 1 limit 200 ". However, you can use laravel collection operations.

https://laravel.com/docs/5.4/eloquent-collections

+1
source

You can use the eloquent MODEL . It returns an object.

ex.

 $user = app\ModelName::all()->count(); 

get (), count () and another aggregate function work with Models

0
source

All Articles