简体   繁体   中英

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.

您必须将数组传递到您的eloquent对象的新实例。

$booking = new Booking($result);

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

you can use eloquent MODEL . It return an object .

ex.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM