Reputation: 6099
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
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
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
Reputation: 1167
You must pass the array into a new instance of your eloquent object.
$booking = new Booking($result);
Upvotes: 1