简体   繁体   中英

laravel api resource collection with row query

$two = DB::select("SELECT
                ul.id ,
                ul.name controller_name, 
                ul.user_id controller_user_id,
                ul.location contoller_location, 
                u.name user_name,
                models.active model_alive_status,
                models.serial_number model_sirial_number,
                su.payment_status subscriptions_status,
                su.to_date subscriptions_expire
    FROM `user_controllers` as ul
    LEFT JOIN users as u ON ul.user_id = u.id
    INNER JOIN models on models.userController_id = ul.id
    LEFT join subscriptions as su on su.user_id = ul.user_id");

return UserControllerInfo::collection($two);

this gives me an error like

"message": "Call to undefined method stdClass::toArray()",

$one = DB::table('user_controllers')
        ->leftJoin('users', 'user_controllers.user_id', '=', 'users.id')
        ->leftJoin('models', 'models.userController_id', '=', 'user_controllers.id')
        ->leftJoin('subscriptions', 'subscriptions.user_id', '=', 'user_controllers.user_id')
        ->select('user_controllers.*','models.*')
        ->get();

this also same error

Query builder will return the collection with stdClass, try to change Query Builder to Eloquent Builder:

$one = UserController::leftJoin('users', 'user_controllers.user_id', '=', 'users.id')
                     ->leftJoin('models', 'models.userController_id', '=', 'user_controllers.id')
                     ->leftJoin('subscriptions', 'subscriptions.user_id', '=', 'user_controllers.user_id')
                     ->select('user_controllers.*','models.*')
                     ->get();

Check my anwser here.

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