在非对象 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
Call to a member function toJson() on a non-object Laravel 5.0
提问by Juliver Galleto
From below code reference, I am joining the users table, user_details
table and user_config
table 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);