ErrorException htmlspecialchars() expects parameter 1 to be string, array given

16
April 16, 2019, at 09:40 AM

I want to get data "report_to" from database. But i got error when I try to print variable in my view page. It's said "ErrorException htmlspecialchars() expects parameter 1 to be string, array given".

This is my controller.

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'));
    }

This is my view

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

I expect the view will display the variable that is "Name", but I got error htmlspecialchars() expects parameter 1 to be string, array given.

Answer 1

Change

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

to

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

and now $reportTo is an array you may need to use @foreach or @for. or if you are sure you will get only one record, you can use first() like

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

it means and it is clear that $reportTo is a results set of the query, change your query to $reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "))->first(); and then access your DB column like $reportTo['colname']

Answer 3

You have to check report_id is array:

<div class="alert alert-info">
        @if(is_array())
            @foreach($reportTo as $rep_to)
             <h3>{{$rep_to}}</h3>
            @endforeach
        @else
           <h3>{{$reportTo}}</h3>
        @endif
    </div>
READ ALSO
How to add Attachment field compulsory in WordPress Add to Cart Page [on hold]

How to add Attachment field compulsory in WordPress Add to Cart Page [on hold]

i have added File attachment field in my add to cart page but if someone did not upload their attachment they are still able to place their orderNow i want to make attachment field compulsory that no one place their order successfully without uploading...

31
synchronous dispatching a job how?

synchronous dispatching a job how?

I am developing a multi tenancy app with laravel 58 and I am using this to set related tenant ID for the job but when I dispatch jobs from console like this

16
header not work shows error header already sent

header not work shows error header already sent

I used header function to sent session values to another page but when I used this code is shows headers already sentI also don't give any blank spaces before starting code and ending code

25
Voyager - Specific Columns to be displayed on browse

Voyager - Specific Columns to be displayed on browse

I am trying to display some specific columns header on browse , i have customised the controller and model , and also have made some changes to index() inside my customised controller

34