为什么Laravel将Db::table查询返回为字符串(5)“;莎莉";


Why does Laravel return a Db::table query as string(5) "Sally"

为什么laravel返回一个信息过多的查询。

为什么要查询:

DB::table('emails')->where('sent', '0')->get();

生成此stdClass条目:

["email"]=> string(21) "webmaster@example.com"

取而代之的是:

["email"]=> "webmaster@example.com"
这是因为Laravel查询返回集合。如果你想得到一个数组,你应该使用toArray()辅助方法。
$collection = DB::table('emails')->where('sent', '0')->get();
$array = $collection->toArray();

get()每次都返回一个对象集合。该集合中可能有0个或多个对象,具体取决于查询的结果。所以,您必须使用toArray()将它们转换为数组。

使用此:

$data = DB::table('emails')->where('sent', '0')->get()->toArray();
print_r($data);