Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php larvel DB:first()方法带来的响应内容必须是字符串_Php_Laravel_Eloquent - Fatal编程技术网

Php larvel DB:first()方法带来的响应内容必须是字符串

Php larvel DB:first()方法带来的响应内容必须是字符串,php,laravel,eloquent,Php,Laravel,Eloquent,我使用Laravel Illumb,当我使用first()方法获得单个结果时,我得到了这个错误: 响应内容必须是一个字符串或对象,它实现了\uuu toString(),给定了“object”。 return DB::table('todos')->where("title","your List")->first(); 如果我使用get()方法选择,它将起作用: return DB::table('todos')->where("title","your List")-&g

我使用Laravel Illumb,当我使用first()方法获得单个结果时,我得到了这个错误:

响应内容必须是一个字符串或对象,它实现了\uuu toString(),给定了“object”。

return DB::table('todos')->where("title","your List")->first();
如果我使用get()方法选择,它将起作用:

return DB::table('todos')->where("title","your List")->get();
你知道第一条语句有什么问题吗?

当你执行
->get()
时,你会得到一个
illumb\Support\Collection
对象。此对象可以由响应返回,因为它实现了
\uu toString()
方法:

/**
 * Convert the collection to its string representation.
 *
 * @return string
 */
public function __toString()
{
    return $this->toJson();
}

/**
 * Get the collection of items as JSON.
 *
 * @param  int  $options
 * @return string
 */
public function toJson($options = 0)
{
    return json_encode($this->jsonSerialize(), $options);
}

/**
 * Convert the object into something JSON serializable.
 *
 * @return array
 */
public function jsonSerialize()
{
    return array_map(function ($value) {
        if ($value instanceof JsonSerializable) {
            return $value->jsonSerialize();
        } elseif ($value instanceof Jsonable) {
            return json_decode($value->toJson(), true);
        } elseif ($value instanceof Arrayable) {
            return $value->toArray();
        } else {
            return $value;
        }
    }, $this->items);
}
如您所见,它所做的一切都是将整个集合转换为json

但是当您执行
->first()
时,幕后发生的事情是,Laravel执行
->take(1)->get()->first()
,因此查询被限制为一行,然后检索包含该行结果的集合,最后返回一个对象

因此,
->first()
调用是在幕后对集合进行的,这意味着您不会得到另一个集合,而是一个数据库对象—可能是
illighted\database\Query\Builder
类型,我记不太清楚了

由于该类没有实现
\uuuToString()
方法,因此响应不知道如何处理它。相反,您会得到一个错误


通过在对象上运行
json\u encode()
,或者返回一个json响应,您可以轻松地模拟相同的响应。

@JoelHinz已经尽可能详细地描述了它。但出于对这种行为的好奇,我在《博士》中发现了一些有趣的东西

使用
DB
Facade并调用
get()
返回(StdClass)对象的集合。由于它位于Laravel的集合中,所以有一些方法可以将其底层属性转换为字符串。但是,当您直接从查询生成器访问集合中的一个项时,就会有一个纯StdClass对象,它没有实现
\uu toString()
(非常确定)

我认为同样的情况是,如果您有一个集合,并且您检索一个底层数组,比如说
$collection[0]
,您将丢失Laravel集合的
\u toString()
的实现,因为您已经打开了它,现在有了纯PHP数组


这种行为正是查询生成器的开发方式。为了更好地处理查询结果,您可以
返回json\u encode($query\u result)
返回响应()->json($query\u result)
,或者使用雄辩(如果您创建了模型),即
返回Todo::where(“title”,“your List”)->first()

当使用
DB
选择一行时,您将得到一个
stdClass
的对象,它没有实现
\uu toString()
。您也可以使用JSON响应<代码>返回响应()->json(compact('todo')