【发布时间】:2017-10-18 00:20:39
【问题描述】:
美好的一天。我最近学习了laravel。然后,我试图从数据库中获取数据(我正在使用 Sqlsrv)但我遇到了一个错误
htmlspecialchars() 期望参数 1 是字符串,给定对象 (查看:D:\xampp\htdocs\boby\resources\views\welcome.blade.php)
这是我的脚本
routes/web.php
Route::get('/', function () {
$datauser = DB::table('users')->get();
return view('welcome',compact('datauser'));
});
而我的观点是这样的(比如welcome.blade.php)
<!doctype html>
<html lang="{{ config('app.locale') }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Raleway', sans-serif;
font-weight: 100;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
text-align: center;
}
.title {
font-size: 84px;
}
.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 12px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}
.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>
<ul>
@foreach ($datauser as $user)
<li> {{ $user }} </li>>
@endforeach
</ul>
</body>
</html>
如何处理这个错误?提前致谢
【问题讨论】:
-
您不能将对象打印为字符串,即使它只有一个值。如果您不知道对象的所有值,您可以像 Sandeesh 解释的那样进行操作,或者将其打印为 json 对象
{{ json_encode($user) }}。