ErrorException htmlspecialchars()期望参数1为字符串,给定数组

时间:2019-04-15 06:55:35

标签: php laravel laravel-5.4

我想从数据库中获取数据“ report_to”。但是当我尝试在视图页面中打印变量时出现错误。据说“ ErrorException htmlspecialchars()期望参数1为字符串,给定数组”。

这是我的控制器。

function editroledetails(Request $request)
    {
        $user = \Auth::user();
        $userphone = 0;
        $reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "));
        $data = [
            'editUsername' => $request->editUsername,
            'editNik' => $request->editNik,
            'editEmail' => $request->editEmail,
            'editRegIdentities' => $request->editRegIdentities,
            'editID' => $request->editID
        ];

        return view('editroledetails', compact('user', 'userphone', 'data', 'reportTo'));
    }

这是我的观点

<div class="alert alert-info">
            <h3>{{$reportTo}}</h3>
        </div>

我希望视图将显示“名称”变量,但是我收到错误htmlspecialchars()期望参数1为字符串,给定数组。

4 个答案:

答案 0 :(得分:0)

更改

$reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "));

$reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "))->get();

,现在$reportTo是一个数组,您可能需要使用@foreach@for。或者,如果您确定仅会获得一条记录,则可以像{p>那样使用first()

$reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "))->first();

答案 1 :(得分:0)

这意味着并且很显然$ reportTo是查询的结果集,将查询更改为$reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "))->first();,然后像$ reportTo ['colname']一样访问数据库列

答案 2 :(得分:0)

您必须检查report_id是否为数组:

<div class="alert alert-info">
        @if(is_array())
            @foreach($reportTo as $rep_to)
             <h3>{{$rep_to}}</h3>
            @endforeach
        @else
           <h3>{{$reportTo}}</h3>
        @endif
    </div>

答案 3 :(得分:0)

[已解决] 谢谢您的回答,我已经解决了。

此代码

<?php echo $reportTo[0]->report_to; ?>

This is print_r of $reportTo

This is my reference