【问题标题】:ErrorException htmlspecialchars() expects parameter 1 to be string, object given ()ErrorException htmlspecialchars() 期望参数 1 是字符串,给定对象 ()
【发布时间】:2020-11-22 15:21:46
【问题描述】:

我的控制器

public function total_record()
{
    $tailor_measurement = DB::select('select count(*) from tailor_measurement ');
    return view('total record', ['tailor_measurement' => $tailor_measurement]);

}

我的浏览总记录

<!DOCTYPE html>
<html>
<head>
    <title>Total Records</title>
</head>
<body>
@foreach($tailor_measurement as $row)
    {{$row}}
@endforeach
</body>
</html>

它给我错误 htmlspecialchars() 期望参数 1 是字符串,给定对象(视图:C:\wamp64\www\shaban\resources\views\total record.blade.php)

【问题讨论】:

  • $row 是一个对象,因此您不能直接在模板中显示它。
  • @Jeto 比我怎么显示?

标签: php laravel laravel-5


【解决方案1】:

当您尝试获取 tailor_measurementcount 时,这将返回多行 integer 值而不是数组,如果您在控制器中使用 dd($tailor_measurement),您会发现它与此类似

array:1 [▼
  0 => {#1555 ▼
    +"count(*)": 35
  }
]

很难访问,你可以像这样简单得多

public function total_record()
{
   $counter = DB::table('tailor_measurement')->count();

   return view('total record', compact('counter'));
}

在你看来

<!DOCTYPE html>
<html>
<head>
<title>Total Records</title>
</head>
<body>
 {{ $counter }}
</body>
</html>

【讨论】:

  • @tajamul hussain 只要解决了您的问题,请将此答案标记为已接受,因此问题将被视为已解决
猜你喜欢
  • 2019-09-05
  • 2018-06-16
  • 2017-08-30
  • 2018-07-30
  • 1970-01-01
  • 2018-11-28
  • 2018-11-27
  • 2021-02-13
  • 2017-03-22
相关资源
最近更新 更多