V4n1ll4
V4n1ll4

Reputation: 6099

Laravel-5 return eloquent object when running MySQL stored procedure

I would like to know if it's possible to return a MySQL stored procedure call as an eloquent object.

The below 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.

Upvotes: 5

Views: 1121

Answers (3)

kenji
kenji

Reputation: 793

A late question, but if anyone else needs this:

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

The only problem is you can't do further sql operations like where, limit, etc, since you can't manipulate a stored procedure directly, eg: you can't do "call bookings_by_voucher() where active = 1 limit 200". You can use laravel collection operations though.

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

Upvotes: 1

Yasin Patel
Yasin Patel

Reputation: 5731

you can use eloquent MODEL. It return an object .

ex.

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

get(),count() and other aggregate function works with Models

Upvotes: 0

Lance Pioch
Lance Pioch

Reputation: 1167

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

$booking = new Booking($result);

Upvotes: 1

Related Questions