【问题标题】:Cannot open view in laravellaravel 无法打开视图
【发布时间】: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) }}

标签: php laravel


【解决方案1】:

你不能像那样显示一个对象。显示时需要获取属性。

<li> {{ $user->id }} </li>>
<li> {{ $user->name }} </li>>
<li> {{ $user->email }} </li>>

【讨论】:

    【解决方案2】:

    您可以使用{{}} 来显示对象。 在foreach 中,$user 是一个对象, 如果要显示用户的电子邮件,请使用它{{ $user-&gt;email }}

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 2018-07-19
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      相关资源
      最近更新 更多