简体   繁体   English

当我尝试从PHP对象/数组返回值时,出现500错误。 但是可以返回所有对象

[英]I get the 500 error when I try to return a value from PHP object/array. But it's ok return all the object

I'm trying to create a simple application in Laravel that sends and gets comments from the database. 我正在尝试在Laravel中创建一个简单的应用程序,该应用程序可以从数据库发送和获取注释。

I'm using both PHP and JS with AJAX. 我在AJAX中同时使用PHP和JS。 But when I try to get the full comment object: 但是,当我尝试获取完整的注释对象时:

PHP PHP

    public function UpdateComment(Request $request){
        $id = $request->id;
        $jsonCode = DB::table('comments')->where('id', $id)->get();
        $comment = json_decode($jsonCode);
        return $comment
    }

Works well: 效果很好:

[{…}]
  0:
   author_id: 6
   comment_date: "2018-09-15 09:53:01"
   comment_text: "23423434234"
   history: ""
   id: 60
   last_update: "2018-09-15 00:00:00"
  >proto__: Object
  length: 1
>proto__: Array(0)

This is exactly what I expected to see, the full PHP object. 这正是我期望看到的完整的PHP对象。 But when I try to return or simply use a single attribute of this same object, I got a 500 error... 但是当我尝试返回或仅使用同一对象的单个属性时,出现了500错误...

return $comment->id;

.

POST http://laravelhost/update-comment 500 (Internal Server Error)

I'm a beginner in PHP, so it should be a very simple error. 我是PHP的初学者,因此应该是一个非常简单的错误。

Try this: 尝试这个:

public function UpdateComment(Request $request){
    $id = $request->id;
    $record = DB::table('comments')->where('id', $id)->get();
    return $record;
}

You don't need to json_decode() the return value of DB::table()... call. 您不需要json_decode()DB :: table()...调用的返回值。

To retrieve a single row you should use first() instead of get() 要检索单行,应使用first()而不是get()

$comment = DB::table('comments')->where('id', $id)->first();
echo $comment->id;

https://laravel.com/docs/5.7/queries https://laravel.com/docs/5.7/queries

You are getting error 500 because json_decode() returns an array, not an object. 由于json_decode()返回一个数组,而不是一个对象,因此出现错误500。 So to access your data use array syntax, instead of object operator: 因此,要使用数组语法而不是对象运算符来访问数据:

return $comment['id']

You can either use 您可以使用

$comment = DB::table('comments')->where('id', $id)->first();
return $comment->id;

or 要么

$comment = DB::table('comments')->where('id', $id)->get();
return $comment[0]->id;

If you are using json_decode after fetching data, object will converted to array. 如果在获取数据后使用json_decode,则对象将转换为数组。 So, you have to use $arrayname['keyname'] for fetching data. 因此,您必须使用$ arrayname ['keyname']来获取数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我尝试编写一个函数,该函数将返回等于数组中传递的数字的所有数组元素的总和。 我该怎么做? - I try to write a function that will return the sum of all array elements that are equal to the number passed in the array. How can I do it? 从与嵌套在对象中的对象的key:value对匹配的数组中返回对象,我需要返回 - Return object from array that matches key:value pair of an object nested in the object I need to return 如何从PHP数组返回JavaScript对象? - How can I return a JavaScript object from a PHP array? 如何从JavaScript中的对象键值返回数组? - How can I return an array from an object key value in JavaScript? 如何从对象中获取键和值并返回数组? - How do I get they keys and the values from the object and return an array? 我想从数组中返回 JSON object - I want to return a JSON object from an array 对于数组中的对象,我很难访问 map() 返回的新数组。 我收到未定义 musicData[i] 的错误 - I am having difficulty accessing the new array return by map(), for objects in an array. I get the error that musicData[i] is not defined 我可以打印状态对象数组,但是当我尝试从数组中的对象访问值时,整个对象未定义 - I can print a state object array, but when I try to access a value from an object in the array, the whole object is undefined getText不返回值,但是当我setValue时返回Object Object - getText does not return the value, but returns Object Object when I setValue object 数组中 object 的返回值 - Return value of an object from array of object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM