在非对象 Laravel 5.0 上调用成员函数 toJson()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32935798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:26:45  来源:igfitidea点击:

Call to a member function toJson() on a non-object Laravel 5.0

phplaravellaravel-5

提问by Juliver Galleto

From below code reference, I am joining the users table, user_detailstable and user_configtable with each record similar key and no problem with that (it works), yet when I'm trying to convert the result records to json array, it gives me this error

从下面的代码参考中,我将用户表、user_details表和user_config表与每个记录相似的键连接起来并且没有问题(它有效),但是当我尝试将结果记录转换为 json 数组时,它给了我这个错误

Call to a member function toJson() on a non-object

在非对象上调用成员函数 toJson()

public function get_users_table(){
       $users = DB::table('users')
        ->join('user_details', 'users.id', '=', 'user_details.id')
        ->join('user_config', 'users.id', '=', 'user_config.id')
        //->select('users.*', 'contacts.phone', 'orders.price')
        ->get()->toJson();

       return response()->json(['success' => true, 'users' => $users]);
    }

Any ideas, help?

任何想法,帮助?

回答by changepicture

get() returns an array not an object. You can simply do json_encode($users);

get() 返回一个数组而不是一个对象。你可以简单地做 json_encode($users);