Tim*_*wis 2 php mysql laravel laravel-5
我在尝试使用DB::rawLaravel查询时遇到了一个问题.当我尝试遍历我的结果集时,我无法定位某个列,因为它的名称包含一个空格:
$result = DB::connection("example")->select(DB::raw(
"SELECT table.column_1 AS 'NameOne',
table.column_2 AS 'Name Two'
FROM example;"
));
Run Code Online (Sandbox Code Playgroud)
如您所见,NameOne不包含空格,但Name Two确实如此.循环遍历结果集时,我无法回显结果table.column_2 AS 'Name Two,因为我无法正确定位它.我尝试过的:
<!-- language: html -->
@foreach($result AS $item)
<tr>
<td>{!! $result->NameOne !!}</td> <!-- Works Fine -->
<td>{!! $result->Name Two !!}</td> <!-- Doesn't work (obvious syntax error) -->
<td>{!! $result->Name_Two !!}</td> <!-- Doesn't work (undefined property) -->
<td>{!! $result->Name+Two !!}</td> <!-- Doesn't work (undefined property) -->
<td>{!! $result->NameTwo !!}</td> <!-- Doesn't work (undefined property) -->
</tr>
@endforeach
Run Code Online (Sandbox Code Playgroud)
具体来说,未定义的属性错误是:
未定义的属性:stdClass :: $ Name_Two(查看:/var/www/html/APPNAME/resources/views/index.blade.php)
我不想更改密钥,因为这些结果将导出到Excel,我想保留列名称.牢记这一点,我将如何回应这些结果?
另外,输出为dd($results):
array:1 [?
0 => {#179 ?
+"NameOne": "NameOne"
+"Name Two": "NameTwo"
}
]
Run Code Online (Sandbox Code Playgroud)