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

我想从数据库中获取数据“ 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为字符串,给定数组。


月关宝盒
浏览 183回答 3
3回答

开满天机

改变$reportTo&nbsp;=&nbsp;DB::select(DB::raw("SELECT&nbsp;report_to&nbsp;FROM&nbsp;customer_type&nbsp;WHERE&nbsp;username='19331986'&nbsp;"));至$reportTo&nbsp;=&nbsp;DB::select(DB::raw("SELECT&nbsp;report_to&nbsp;FROM&nbsp;customer_type&nbsp;WHERE&nbsp;username='19331986'&nbsp;"))->get();现在$reportTo是您可能需要使用@foreach或的数组@for。或者,如果你相信你会得到一个记录,你可以使用first()像$reportTo&nbsp;=&nbsp;DB::select(DB::raw("SELECT&nbsp;report_to&nbsp;FROM&nbsp;customer_type&nbsp;WHERE&nbsp;username='19331986'&nbsp;"))->first();

慕仙森

您必须检查report_id是否为数组:<div class="alert alert-info">&nbsp; &nbsp; &nbsp; &nbsp; @if(is_array())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($reportTo as $rep_to)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<h3>{{$rep_to}}</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; @else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<h3>{{$reportTo}}</h3>&nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; </div>
打开App,查看更多内容
随时随地看视频慕课网APP