5

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.

1
  • I am wondering EXACTLY this. Unfortunately I have inherited a gigantic Piece of dung database that I cannot redo due to money / time constraints. Being able to do the above would simplify SO MUCH for me Commented Oct 13, 2015 at 16:24

3 Answers 3

1

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

$booking = new Booking($result);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

you can use eloquent MODEL. It return an object .

ex.

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

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

Comments

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.